blob: 8683e15269cc68aded6e1f529d49de58f19ffabb [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 Warsaw755c6711996-08-01 20:02:55 +000021
Barry Warsaw7b0f5681995-03-08 21:33:04 +000022;; This is a major mode for editing Python programs. It was developed
Barry Warsaw755c6711996-08-01 20:02:55 +000023;; by Tim Peters after an original idea by Michael A. Guravage. Tim
24;; left the net for a while and in the interim, Barry Warsaw has
25;; undertaken maintenance of the mode.
Barry Warsaw7b0f5681995-03-08 21:33:04 +000026
27;; At some point this mode will undergo a rewrite to bring it more in
Barry Warsaw755c6711996-08-01 20:02:55 +000028;; line with GNU Emacs Lisp coding standards, and to wax all the Emacs
29;; 18 support. But all in all, the mode works exceedingly well, and
30;; I've simply been tweaking it as I go along. Ain't it wonderful
31;; that Python has a much more sane syntax than C? (or <shudder> C++?!
32;; :-). I can say that; I maintain cc-mode!
Barry Warsaw7b0f5681995-03-08 21:33:04 +000033
34;; The following statements, placed in your .emacs file or
35;; site-init.el, will cause this file to be autoloaded, and
36;; python-mode invoked, when visiting .py files (assuming this file is
37;; in your load-path):
Barry Warsaw7ae77681994-12-12 20:38:05 +000038;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +000039;; (autoload 'python-mode "python-mode" "Python editing mode." t)
Barry Warsaw7ae77681994-12-12 20:38:05 +000040;; (setq auto-mode-alist
41;; (cons '("\\.py$" . python-mode) auto-mode-alist))
Barry Warsaw44b72201996-07-05 20:11:35 +000042;;
43;; If you want font-lock support for Python source code (a.k.a. syntax
44;; coloring, highlighting), add this to your .emacs file:
45;;
46;; (add-hook 'python-mode-hook 'turn-on-font-lock)
Barry Warsawc08a9491996-07-31 22:27:58 +000047;;
48;; But you better be sure you're version of Emacs supports
49;; font-lock-mode! As of this writing, the latest Emacs and XEmacs
50;; 19's do.
Barry Warsaw7ae77681994-12-12 20:38:05 +000051
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000052;; Here's a brief list of recent additions/improvements:
53;;
54;; - Wrapping and indentation within triple quote strings should work
55;; properly now.
56;; - `Standard' bug reporting mechanism (use C-c C-b)
57;; - py-mark-block was moved to C-c C-m
58;; - C-c C-v shows you the python-mode version
59;; - a basic python-font-lock-keywords has been added for Emacs 19
60;; font-lock colorizations.
61;; - proper interaction with pending-del and del-sel modes.
62;; - New py-electric-colon (:) command for improved outdenting. Also
63;; py-indent-line (TAB) should handle outdented lines better.
Barry Warsaw03970731996-07-03 23:12:52 +000064;; - improved (I think) C-c > and C-c <
Barry Warsaw9e5a9c81996-07-24 18:26:53 +000065;; - py-(forward|backward)-into-nomenclature, not bound, but useful on
66;; M-f and M-b respectively.
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000067
Barry Warsaw7b0f5681995-03-08 21:33:04 +000068;; Here's a brief to do list:
69;;
Barry Warsawb5e0ecb1995-03-14 18:32:54 +000070;; - Better integration with gud-mode for debugging.
71;; - Rewrite according to GNU Emacs Lisp standards.
72;; - py-delete-char should obey numeric arguments.
Barry Warsaw7a1f6f41995-05-08 21:36:20 +000073;; - de-electrify colon inside literals (e.g. comments and strings)
Barry Warsaw5c0d00f1996-07-31 21:30:21 +000074;; - possibly force indent-tabs-mode == nil, and add a
75;; write-file-hooks that runs untabify on the whole buffer (to work
76;; around potential tab/space mismatch problems). In practice this
77;; hasn't been a problem... yet.
Barry Warsaw9e277db1996-07-31 22:33:40 +000078;; - have py-execute-region on indented code act as if the region is
79;; left justified. Avoids syntax errors.
Barry Warsaw7ae77681994-12-12 20:38:05 +000080
Barry Warsaw7b0f5681995-03-08 21:33:04 +000081;; If you can think of more things you'd like to see, drop me a line.
82;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
83;;
Barry Warsaw4669d7e1996-03-22 16:13:24 +000084;; Note that I only test things on XEmacs. If you port stuff to FSF
85;; Emacs 19, or Emacs 18, please send me your patches. Byte compiler
86;; complaints can probably be safely ignored.
Barry Warsaw7ae77681994-12-12 20:38:05 +000087
Barry Warsaw7b0f5681995-03-08 21:33:04 +000088;; LCD Archive Entry:
Barry Warsawfec75d61995-07-05 23:26:15 +000089;; python-mode|Barry A. Warsaw|python-mode@python.org
Barry Warsaw7b0f5681995-03-08 21:33:04 +000090;; |Major mode for editing Python programs
91;; |$Date$|$Revision$|
Barry Warsaw7ae77681994-12-12 20:38:05 +000092
Barry Warsaw7b0f5681995-03-08 21:33:04 +000093;;; Code:
94
95
96;; user definable variables
97;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Barry Warsaw7ae77681994-12-12 20:38:05 +000098
99(defvar py-python-command "python"
100 "*Shell command used to start Python interpreter.")
101
Barry Warsaw17914f41995-11-03 18:25:15 +0000102(defvar py-indent-offset 4
Barry Warsaw7ae77681994-12-12 20:38:05 +0000103 "*Indentation increment.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000104Note that `\\[py-guess-indent-offset]' can usually guess a good value
105when you're editing someone else's Python code.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000106
Barry Warsaw095e9c61995-09-19 20:01:42 +0000107(defvar py-align-multiline-strings-p t
108 "*Flag describing how multiline triple quoted strings are aligned.
109When this flag is non-nil, continuation lines are lined up under the
110preceding line's indentation. When this flag is nil, continuation
111lines are aligned to column zero.")
112
Barry Warsaw7ae77681994-12-12 20:38:05 +0000113(defvar py-block-comment-prefix "##"
Barry Warsaw867a32a1996-03-07 18:30:26 +0000114 "*String used by \\[comment-region] to comment out a block of code.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000115This should follow the convention for non-indenting comment lines so
116that the indentation commands won't get confused (i.e., the string
117should be of the form `#x...' where `x' is not a blank or a tab, and
118`...' is arbitrary).")
119
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000120(defvar py-honor-comment-indentation t
Barry Warsaw6d627751996-03-06 18:41:38 +0000121 "*Controls how comment lines influence subsequent indentation.
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000122
Barry Warsaw6d627751996-03-06 18:41:38 +0000123When nil, all comment lines are skipped for indentation purposes, and
124in Emacs 19, a faster algorithm is used.
125
126When t, lines that begin with a single `#' are a hint to subsequent
127line indentation. If the previous line is such a comment line (as
128opposed to one that starts with `py-block-comment-prefix'), then it's
129indentation is used as a hint for this line's indentation. Lines that
130begin with `py-block-comment-prefix' are ignored for indentation
131purposes.
132
133When not nil or t, comment lines that begin with a `#' are used as
134indentation hints, unless the comment character is in column zero.")
Barry Warsaw33d6ec01996-03-05 16:28:07 +0000135
Barry Warsaw7ae77681994-12-12 20:38:05 +0000136(defvar py-scroll-process-buffer t
137 "*Scroll Python process buffer as output arrives.
138If nil, the Python process buffer acts, with respect to scrolling, like
139Shell-mode buffers normally act. This is surprisingly complicated and
140so won't be explained here; in fact, you can't get the whole story
141without studying the Emacs C code.
142
143If non-nil, the behavior is different in two respects (which are
144slightly inaccurate in the interest of brevity):
145
146 - If the buffer is in a window, and you left point at its end, the
147 window will scroll as new output arrives, and point will move to the
148 buffer's end, even if the window is not the selected window (that
149 being the one the cursor is in). The usual behavior for shell-mode
150 windows is not to scroll, and to leave point where it was, if the
151 buffer is in a window other than the selected window.
152
153 - If the buffer is not visible in any window, and you left point at
154 its end, the buffer will be popped into a window as soon as more
155 output arrives. This is handy if you have a long-running
156 computation and don't want to tie up screen area waiting for the
157 output. The usual behavior for a shell-mode buffer is to stay
158 invisible until you explicitly visit it.
159
160Note the `and if you left point at its end' clauses in both of the
161above: you can `turn off' the special behaviors while output is in
162progress, by visiting the Python buffer and moving point to anywhere
163besides the end. Then the buffer won't scroll, point will remain where
164you leave it, and if you hide the buffer it will stay hidden until you
165visit it again. You can enable and disable the special behaviors as
166often as you like, while output is in progress, by (respectively) moving
167point to, or away from, the end of the buffer.
168
169Warning: If you expect a large amount of output, you'll probably be
170happier setting this option to nil.
171
172Obscure: `End of buffer' above should really say `at or beyond the
173process mark', but if you know what that means you didn't need to be
174told <grin>.")
175
176(defvar py-temp-directory
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000177 (let ((ok '(lambda (x)
178 (and x
179 (setq x (expand-file-name x)) ; always true
180 (file-directory-p x)
181 (file-writable-p x)
182 x))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000183 (or (funcall ok (getenv "TMPDIR"))
184 (funcall ok "/usr/tmp")
185 (funcall ok "/tmp")
186 (funcall ok ".")
187 (error
188 "Couldn't find a usable temp directory -- set py-temp-directory")))
189 "*Directory used for temp files created by a *Python* process.
190By default, the first directory from this list that exists and that you
191can write into: the value (if any) of the environment variable TMPDIR,
192/usr/tmp, /tmp, or the current directory.")
193
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000194(defvar py-beep-if-tab-change t
195 "*Ring the bell if tab-width is changed.
196If a comment of the form
197
198 \t# vi:set tabsize=<number>:
199
200is found before the first code line when the file is entered, and the
201current value of (the general Emacs variable) `tab-width' does not
202equal <number>, `tab-width' is set to <number>, a message saying so is
203displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
204the Emacs bell is also rung as a warning.")
205
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000206(defconst python-font-lock-keywords
Barry Warsaw44b72201996-07-05 20:11:35 +0000207 (let* ((keywords '("access" "and" "break" "class"
208 "continue" "def" "del" "elif"
209 "else:" "except" "except:" "exec"
210 "finally:" "for" "from" "global"
211 "if" "import" "in" "is"
212 "lambda" "not" "or" "pass"
213 "print" "raise" "return" "try:"
214 "while"
Barry Warsaw33ab6e41996-03-05 00:44:31 +0000215 ))
216 (kwregex (mapconcat 'identity keywords "\\|")))
217 (list
218 ;; keywords not at beginning of line
219 (cons (concat "\\s-\\(" kwregex "\\)[ \n\t(]") 1)
220 ;; keywords at beginning of line. i don't think regexps are
221 ;; powerful enough to handle these two cases in one regexp.
222 ;; prove me wrong!
223 (cons (concat "^\\(" kwregex "\\)[ \n\t(]") 1)
224 ;; classes
225 '("\\bclass[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
226 1 font-lock-type-face)
227 ;; functions
228 '("\\bdef[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
229 1 font-lock-function-name-face)
230 ))
Barry Warsaw62d9d6e1996-03-06 20:32:27 +0000231 "Additional expressions to highlight in Python mode.")
Barry Warsawb01b4fa1995-06-20 18:55:34 +0000232
Barry Warsaw81437461996-08-01 19:48:02 +0000233(defvar imenu-example--python-show-method-args-p nil
234 "*Controls echoing of arguments of functions & methods in the imenu buffer.
235When non-nil, arguments are printed.")
236
237
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000238
239;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
240;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
241
Barry Warsaw52bc17c1995-10-12 21:15:49 +0000242(make-variable-buffer-local 'py-indent-offset)
243
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000244;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
245;; seems to be the standard way of checking this.
246;; BAW - This is *not* the right solution. When at all possible,
247;; instead of testing for the version of Emacs, use feature tests.
248
249(setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
250(setq py-this-is-emacs-19-p
251 (and
252 (not py-this-is-lucid-emacs-p)
253 (string-match "^19\\." emacs-version)))
254
Barry Warsaw7ae77681994-12-12 20:38:05 +0000255;; have to bind py-file-queue before installing the kill-emacs hook
256(defvar py-file-queue nil
257 "Queue of Python temp files awaiting execution.
258Currently-active file is at the head of the list.")
259
260;; define a mode-specific abbrev table for those who use such things
261(defvar python-mode-abbrev-table nil
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000262 "Abbrev table in use in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000263(define-abbrev-table 'python-mode-abbrev-table nil)
264
Barry Warsaw7ae77681994-12-12 20:38:05 +0000265(defvar python-mode-hook nil
266 "*Hook called by `python-mode'.")
267
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000268;; in previous version of python-mode.el, the hook was incorrectly
269;; called py-mode-hook, and was not defvar'd. deprecate its use.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000270(and (fboundp 'make-obsolete-variable)
271 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
272
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000273(defvar py-mode-map ()
274 "Keymap used in `python-mode' buffers.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000275
Barry Warsaw7ae77681994-12-12 20:38:05 +0000276(if py-mode-map
277 ()
278 (setq py-mode-map (make-sparse-keymap))
279
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000280 ;; shadow global bindings for newline-and-indent w/ the py- version.
281 ;; BAW - this is extremely bad form, but I'm not going to change it
282 ;; for now.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000283 (mapcar (function (lambda (key)
284 (define-key
285 py-mode-map key 'py-newline-and-indent)))
286 (where-is-internal 'newline-and-indent))
287
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000288 ;; BAW - you could do it this way, but its not considered proper
289 ;; major-mode form.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000290 (mapcar (function
291 (lambda (x)
292 (define-key py-mode-map (car x) (cdr x))))
Barry Warsawb91b7431995-03-14 15:55:20 +0000293 '((":" . py-electric-colon)
294 ("\C-c\C-c" . py-execute-buffer)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000295 ("\C-c|" . py-execute-region)
296 ("\C-c!" . py-shell)
297 ("\177" . py-delete-char)
298 ("\n" . py-newline-and-indent)
299 ("\C-c:" . py-guess-indent-offset)
300 ("\C-c\t" . py-indent-region)
Barry Warsawdea4a291996-07-03 22:59:12 +0000301 ("\C-c\C-l" . py-shift-region-left)
302 ("\C-c\C-r" . py-shift-region-right)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000303 ("\C-c<" . py-shift-region-left)
304 ("\C-c>" . py-shift-region-right)
305 ("\C-c\C-n" . py-next-statement)
306 ("\C-c\C-p" . py-previous-statement)
307 ("\C-c\C-u" . py-goto-block-up)
Barry Warsaw850437a1995-03-08 21:50:28 +0000308 ("\C-c\C-m" . py-mark-block)
Barry Warsawa7891711996-08-01 15:53:09 +0000309 ("\C-c#" . py-comment-region)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000310 ("\C-c?" . py-describe-mode)
311 ("\C-c\C-hm" . py-describe-mode)
312 ("\e\C-a" . beginning-of-python-def-or-class)
313 ("\e\C-e" . end-of-python-def-or-class)
Barry Warsaw850437a1995-03-08 21:50:28 +0000314 ( "\e\C-h" . mark-python-def-or-class)))
315 ;; should do all keybindings this way
316 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
317 (define-key py-mode-map "\C-c\C-v" 'py-version)
318 )
Barry Warsaw7ae77681994-12-12 20:38:05 +0000319
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000320(defvar py-mode-syntax-table nil
321 "Syntax table used in `python-mode' buffers.")
322
Barry Warsaw7ae77681994-12-12 20:38:05 +0000323(if py-mode-syntax-table
324 ()
325 (setq py-mode-syntax-table (make-syntax-table))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000326 ;; BAW - again, blech.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000327 (mapcar (function
328 (lambda (x) (modify-syntax-entry
329 (car x) (cdr x) py-mode-syntax-table)))
330 '(( ?\( . "()" ) ( ?\) . ")(" )
331 ( ?\[ . "(]" ) ( ?\] . ")[" )
332 ( ?\{ . "(}" ) ( ?\} . "){" )
333 ;; fix operator symbols misassigned in the std table
334 ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
335 ( ?\* . "." ) ( ?\+ . "." ) ( ?\- . "." )
336 ( ?\/ . "." ) ( ?\< . "." ) ( ?\= . "." )
337 ( ?\> . "." ) ( ?\| . "." )
Barry Warsawfb349421996-07-24 18:32:08 +0000338 ;; for historical reasons, underscore is word class
339 ;; instead of symbol class. it should be symbol class,
340 ;; but if you're tempted to change it, try binding M-f and
341 ;; M-b to py-forward-into-nomenclature and
342 ;; py-backward-into-nomenclature instead. -baw
Barry Warsaw8e9d7d71996-07-03 23:15:51 +0000343 ( ?\_ . "w" ) ; underscore is legit in words
Barry Warsaw7ae77681994-12-12 20:38:05 +0000344 ( ?\' . "\"") ; single quote is string quote
345 ( ?\" . "\"" ) ; double quote is string quote too
346 ( ?\` . "$") ; backquote is open and close paren
347 ( ?\# . "<") ; hash starts comment
348 ( ?\n . ">")))) ; newline ends comment
349
350(defconst py-stringlit-re
351 (concat
352 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
353 "\\|" ; or
354 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000355 "Regexp matching a Python string literal.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000356
357;; this is tricky because a trailing backslash does not mean
358;; continuation if it's in a comment
359(defconst py-continued-re
360 (concat
361 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
362 "\\\\$")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000363 "Regexp matching Python lines that are continued via backslash.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000364
365(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000366 "Regexp matching blank or comment lines.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000367
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000368(defconst py-outdent-re
369 (concat "\\(" (mapconcat 'identity
370 '("else:"
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000371 "except\\(\\s +.*\\)?:"
Barry Warsaw0012c1e1995-03-14 16:32:55 +0000372 "finally:"
373 "elif\\s +.*:")
374 "\\|")
375 "\\)")
376 "Regexp matching clauses to be outdented one level.")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000377
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000378(defconst py-no-outdent-re
379 (concat "\\(" (mapconcat 'identity
Barry Warsaw464c94a1995-03-14 23:25:44 +0000380 '("try:"
Barry Warsaw4f009fb1995-03-14 20:53:08 +0000381 "except\\(\\s +.*\\)?:"
382 "while\\s +.*:"
383 "for\\s +.*:"
384 "if\\s +.*:"
385 "elif\\s +.*:")
386 "\\|")
387 "\\)")
388 "Regexp matching lines to not outdent after.")
389
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000390
Barry Warsaw42f707f1996-07-29 21:05:05 +0000391;; Menu definitions, only relevent if you have the easymenu.el package
392;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
393(if (condition-case nil
394 (require 'easymenu)
395 (error nil))
396 (easy-menu-define
397 py-menu py-mode-map "Python Mode menu"
398 '("Python"
399 ["Comment Out Region" comment-region (mark)]
400 ["Uncomment Region" (comment-region (point) (mark) '(4)) (mark)]
401 "-"
402 ["Mark current block" py-mark-block t]
403 ["Mark current def" mark-python-def-or-class t]
404 ["Mark current class" (mark-python-def-or-class t) t]
405 "-"
406 ["Shift region left" py-shift-region-left (mark)]
407 ["Shift region right" py-shift-region-right (mark)]
408 "-"
409 ["Execute buffer" py-execute-buffer t]
410 ["Execute region" py-execute-region (mark)]
411 ["Start interpreter..." py-shell t]
412 "-"
413 ["Go to start of block" py-goto-block-up t]
414 ["Go to start of class" (beginning-of-python-def-or-class t) t]
415 ["Move to end of class" (end-of-python-def-or-class t) t]
416 ["Move to start of def" beginning-of-python-def-or-class t]
417 ["Move to end of def" end-of-python-def-or-class t]
418 "-"
419 ["Describe mode" py-describe-mode t]
420 )))
421
Barry Warsaw81437461996-08-01 19:48:02 +0000422
423
424;; imenu definitions, courtesy of Perry A. Stoll <stoll@atr-sw.atr.co.jp>
425(defvar imenu-example--python-class-regexp
426 (concat ; <<classes>>
427 "\\(" ;
428 "^[ \t]*" ; newline and maybe whitespace
429 "\\(class[ \t]+[a-zA-Z0-9_]+\\)" ; class name
430 ; possibly multiple superclasses
431 "\\([ \t]*\\((\\([a-zA-Z0-9_, \t\n]\\)*)\\)?\\)"
432 "[ \t]*:" ; and the final :
433 "\\)" ; >>classes<<
434 )
435 "Regexp for Python classes for use with the imenu package."
436 )
437
438(defvar imenu-example--python-method-regexp
439 (concat ; <<methods and functions>>
440 "\\(" ;
441 "^[ \t]*" ; new line and maybe whitespace
442 "\\(def[ \t]+" ; function definitions start with def
443 "\\([a-zA-Z0-9_]+\\)" ; name is here
444 ; function arguments...
445 "[ \t]*(\\([a-zA-Z0-9_=,\* \t\n]*\\))"
446 "\\)" ; end of def
447 "[ \t]*:" ; and then the :
448 "\\)" ; >>methods and functions<<
449 )
450 "Regexp for Python methods/functions for use with the imenu package."
451 )
452
453(defvar imenu-example--python-method-no-arg-parens '(2 8)
454 "Indicies into groups of the Python regexp for use with imenu.
455
456Using these values will result in smaller imenu lists, as arguments to
457functions are not listed.
458
459See the variable `imenu-example--python-show-method-args-p' for more
460information.")
461
462(defvar imenu-example--python-method-arg-parens '(2 7)
463 "Indicies into groups of the Python regexp for use with imenu.
464Using these values will result in large imenu lists, as arguments to
465functions are listed.
466
467See the variable `imenu-example--python-show-method-args-p' for more
468information.")
469
470;; Note that in this format, this variable can still be used with the
471;; imenu--generic-function. Otherwise, there is no real reason to have
472;; it.
473(defvar imenu-example--generic-python-expression
474 (cons
475 (concat
476 imenu-example--python-class-regexp
477 "\\|" ; or...
478 imenu-example--python-method-regexp
479 )
480 imenu-example--python-method-no-arg-parens)
481 "Generic Python expression which may be used directly with imenu.
482Used by setting the variable `imenu-generic-expression' to this value.
483Also, see the function \\[imenu-example--create-python-index] for a
484better alternative for finding the index.")
485
486;; These next two variables are used when searching for the python
487;; class/definitions. Just saving some time in accessing the
488;; generic-python-expression, really.
489(defvar imenu-example--python-generic-regexp)
490(defvar imenu-example--python-generic-parens)
491
492
493;;;###autoload
494(eval-when-compile
495 ;; Imenu isn't used in XEmacs, so just ignore load errors
496 (condition-case ()
497 (progn
498 (require 'cl)
499 (require 'imenu))
500 (error nil)))
501
502(defun imenu-example--create-python-index ()
503 "Python interface function for imenu package.
504Finds all python classes and functions/methods. Calls function
505\\[imenu-example--create-python-index-engine]. See that function for
506the details of how this works."
507 (setq imenu-example--python-generic-regexp
508 (car imenu-example--generic-python-expression))
509 (setq imenu-example--python-generic-parens
510 (if imenu-example--python-show-method-args-p
511 imenu-example--python-method-arg-parens
512 imenu-example--python-method-no-arg-parens))
513 (goto-char (point-min))
514 (imenu-example--create-python-index-engine nil))
515
516(defun imenu-example--create-python-index-engine (&optional start-indent)
517 "Function for finding imenu definitions in Python.
518
519Finds all definitions (classes, methods, or functions) in a Python
520file for the imenu package.
521
522Returns a possibly nested alist of the form
523
524 (INDEX-NAME . INDEX-POSITION)
525
526The second element of the alist may be an alist, producing a nested
527list as in
528
529 (INDEX-NAME . INDEX-ALIST)
530
531This function should not be called directly, as it calls itself
532recursively and requires some setup. Rather this is the engine for
533the function \\[imenu-example--create-python-index].
534
535It works recursively by looking for all definitions at the current
536indention level. When it finds one, it adds it to the alist. If it
537finds a definition at a greater indentation level, it removes the
538previous definition from the alist. In it's place it adds all
539definitions found at the next indentation level. When it finds a
540definition that is less indented then the current level, it retuns the
541alist it has created thus far.
542
543The optional argument START-INDENT indicates the starting indentation
544at which to continue looking for Python classes, methods, or
545functions. If this is not supplied, the function uses the indentation
546of the first definition found."
547 (let ((index-alist '())
548 (sub-method-alist '())
549 looking-p
550 def-name prev-name
551 cur-indent def-pos
552 (class-paren (first imenu-example--python-generic-parens))
553 (def-paren (second imenu-example--python-generic-parens)))
554 (setq looking-p
555 (re-search-forward imenu-example--python-generic-regexp
556 (point-max) t))
557 (while looking-p
558 (save-excursion
559 ;; used to set def-name to this value but generic-extract-name is
560 ;; new to imenu-1.14. this way it still works with imenu-1.11
561 ;;(imenu--generic-extract-name imenu-example--python-generic-parens))
562 (let ((cur-paren (if (match-beginning class-paren)
563 class-paren def-paren)))
564 (setq def-name
565 (buffer-substring (match-beginning cur-paren)
566 (match-end cur-paren))))
567 (beginning-of-line)
568 (setq cur-indent (current-indentation)))
569
570 ;; HACK: want to go to the next correct definition location. we
571 ;; explicitly list them here. would be better to have them in a
572 ;; list.
573 (setq def-pos
574 (or (match-beginning class-paren)
575 (match-beginning def-paren)))
576
577 ;; if we don't have a starting indent level, take this one
578 (or start-indent
579 (setq start-indent cur-indent))
580
581 ;; if we don't have class name yet, take this one
582 (or prev-name
583 (setq prev-name def-name))
584
585 ;; what level is the next definition on? must be same, deeper
586 ;; or shallower indentation
587 (cond
588 ;; at the same indent level, add it to the list...
589 ((= start-indent cur-indent)
590
591 ;; if we don't have push, use the following...
592 ;;(setf index-alist (cons (cons def-name def-pos) index-alist))
593 (push (cons def-name def-pos) index-alist))
594
595 ;; deeper indented expression, recur...
596 ((< start-indent cur-indent)
597
598 ;; the point is currently on the expression we're supposed to
599 ;; start on, so go back to the last expression. The recursive
600 ;; call will find this place again and add it to the correct
601 ;; list
602 (re-search-backward imenu-example--python-generic-regexp
603 (point-min) 'move)
604 (setq sub-method-alist (imenu-example--create-python-index-engine
605 cur-indent))
606
607 (if sub-method-alist
608 ;; we put the last element on the index-alist on the start
609 ;; of the submethod alist so the user can still get to it.
610 (let ((save-elmt (pop index-alist)))
611 (push (cons (imenu-create-submenu-name prev-name)
612 (cons save-elmt sub-method-alist))
613 index-alist))))
614
615 ;; found less indented expression, we're done.
616 (t
617 (setq looking-p nil)
618 (re-search-backward imenu-example--python-generic-regexp
619 (point-min) t)))
620 (setq prev-name def-name)
621 (and looking-p
622 (setq looking-p
623 (re-search-forward imenu-example--python-generic-regexp
624 (point-max) 'move))))
625 (nreverse index-alist)))
626
Barry Warsaw42f707f1996-07-29 21:05:05 +0000627
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000628;;;###autoload
Barry Warsaw7ae77681994-12-12 20:38:05 +0000629(defun python-mode ()
630 "Major mode for editing Python files.
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000631To submit a problem report, enter `\\[py-submit-bug-report]' from a
632`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
633documentation. To see what version of `python-mode' you are running,
634enter `\\[py-version]'.
635
636This mode knows about Python indentation, tokens, comments and
637continuation lines. Paragraphs are separated by blank lines only.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000638
639COMMANDS
640\\{py-mode-map}
641VARIABLES
642
Barry Warsaw42f707f1996-07-29 21:05:05 +0000643py-indent-offset\t\tindentation increment
644py-block-comment-prefix\t\tcomment string used by comment-region
645py-python-command\t\tshell command to invoke Python interpreter
646py-scroll-process-buffer\t\talways scroll Python process buffer
647py-temp-directory\t\tdirectory used for temp files (if needed)
648py-beep-if-tab-change\t\tring the bell if tab-width is changed"
Barry Warsaw7ae77681994-12-12 20:38:05 +0000649 (interactive)
650 (kill-all-local-variables)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000651 (set-syntax-table py-mode-syntax-table)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000652 (setq major-mode 'python-mode
653 mode-name "Python"
654 local-abbrev-table python-mode-abbrev-table)
655 (use-local-map py-mode-map)
Barry Warsaw42f707f1996-07-29 21:05:05 +0000656 ;; add the menu
657 (if py-menu
658 (easy-menu-add py-menu))
Barry Warsaw57697af1995-09-14 20:01:14 +0000659 ;; Emacs 19 requires this
660 (if (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p)
661 (setq comment-multi-line nil))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000662 ;; BAW -- style...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000663 (mapcar (function (lambda (x)
664 (make-local-variable (car x))
665 (set (car x) (cdr x))))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000666 '((paragraph-separate . "^[ \t]*$")
667 (paragraph-start . "^[ \t]*$")
668 (require-final-newline . t)
Barry Warsawa7891711996-08-01 15:53:09 +0000669 (comment-start . "# ")
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000670 (comment-start-skip . "# *")
671 (comment-column . 40)
672 (indent-region-function . py-indent-region)
673 (indent-line-function . py-indent-line)))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000674 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000675 ;;
676 ;; not sure where the magic comment has to be; to save time
677 ;; searching for a rarity, we give up if it's not found prior to the
678 ;; first executable statement.
679 ;;
680 ;; BAW - on first glance, this seems like complete hackery. Why was
681 ;; this necessary, and is it still necessary?
682 (let ((case-fold-search nil)
683 (start (point))
684 new-tab-width)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000685 (if (re-search-forward
686 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
687 (prog2 (py-next-statement 1) (point) (goto-char 1))
688 t)
689 (progn
690 (setq new-tab-width
691 (string-to-int
692 (buffer-substring (match-beginning 1) (match-end 1))))
693 (if (= tab-width new-tab-width)
694 nil
695 (setq tab-width new-tab-width)
696 (message "Caution: tab-width changed to %d" new-tab-width)
697 (if py-beep-if-tab-change (beep)))))
698 (goto-char start))
699
Barry Warsaw755c6711996-08-01 20:02:55 +0000700 ;; install imenu
701 (setq imenu-create-index-function
702 (function imenu-example--create-python-index))
703 (if (fboundp 'imenu-add-to-menubar)
704 (imenu-add-to-menubar (format "%s-%s" "IM" mode-name)))
705
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000706 ;; run the mode hook. py-mode-hook use is deprecated
Barry Warsaw7ae77681994-12-12 20:38:05 +0000707 (if python-mode-hook
708 (run-hooks 'python-mode-hook)
709 (run-hooks 'py-mode-hook)))
710
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000711
Barry Warsaw826255b1996-03-22 16:09:34 +0000712(defun py-keep-region-active ()
Barry Warsawce60bc71996-08-01 18:17:14 +0000713 ;; do whatever is necessary to keep the region active in XEmacs.
714 ;; Ignore byte-compiler warnings you might see. Also note that
715 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
716 ;; require us to take explicit action.
Barry Warsaw826255b1996-03-22 16:09:34 +0000717 (and (boundp 'zmacs-region-stays)
718 (setq zmacs-region-stays t)))
719
Barry Warsawce60bc71996-08-01 18:17:14 +0000720
Barry Warsawb91b7431995-03-14 15:55:20 +0000721;; electric characters
Barry Warsaw3874a3d1995-03-14 22:05:53 +0000722(defun py-outdent-p ()
723 ;; returns non-nil if the current line should outdent one level
724 (save-excursion
725 (and (progn (back-to-indentation)
726 (looking-at py-outdent-re))
727 (progn (backward-to-indentation 1)
728 (while (or (looking-at py-blank-or-comment-re)
729 (bobp))
730 (backward-to-indentation 1))
731 (not (looking-at py-no-outdent-re)))
732 )))
733
734
Barry Warsawb91b7431995-03-14 15:55:20 +0000735(defun py-electric-colon (arg)
736 "Insert a colon.
737In certain cases the line is outdented appropriately. If a numeric
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000738argument is provided, that many colons are inserted non-electrically.
739Electric behavior is inhibited inside a string or comment."
Barry Warsawb91b7431995-03-14 15:55:20 +0000740 (interactive "P")
741 (self-insert-command (prefix-numeric-value arg))
Barry Warsaw0c6563f1995-09-14 20:57:02 +0000742 ;; are we in a string or comment?
743 (if (save-excursion
744 (let ((pps (parse-partial-sexp (save-excursion
745 (beginning-of-python-def-or-class)
746 (point))
747 (point))))
748 (not (or (nth 3 pps) (nth 4 pps)))))
749 (save-excursion
750 (let ((here (point))
751 (outdent 0)
752 (indent (py-compute-indentation)))
753 (if (and (not arg)
754 (py-outdent-p)
755 (= indent (save-excursion
756 (forward-line -1)
757 (py-compute-indentation)))
758 )
759 (setq outdent py-indent-offset))
760 ;; Don't indent, only outdent. This assumes that any lines that
761 ;; are already outdented relative to py-compute-indentation were
762 ;; put there on purpose. Its highly annoying to have `:' indent
763 ;; for you. Use TAB, C-c C-l or C-c C-r to adjust. TBD: Is
764 ;; there a better way to determine this???
765 (if (< (current-indentation) indent) nil
766 (goto-char here)
767 (beginning-of-line)
768 (delete-horizontal-space)
769 (indent-to (- indent outdent))
770 )))))
Barry Warsawb91b7431995-03-14 15:55:20 +0000771
772
Barry Warsaw7ae77681994-12-12 20:38:05 +0000773;;; Functions that execute Python commands in a subprocess
Barry Warsaw7ae77681994-12-12 20:38:05 +0000774(defun py-shell ()
775 "Start an interactive Python interpreter in another window.
776This is like Shell mode, except that Python is running in the window
777instead of a shell. See the `Interactive Shell' and `Shell Mode'
778sections of the Emacs manual for details, especially for the key
779bindings active in the `*Python*' buffer.
780
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000781See the docs for variable `py-scroll-buffer' for info on scrolling
Barry Warsaw7ae77681994-12-12 20:38:05 +0000782behavior in the process window.
783
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000784Warning: Don't use an interactive Python if you change sys.ps1 or
785sys.ps2 from their default values, or if you're running code that
786prints `>>> ' or `... ' at the start of a line. `python-mode' can't
787distinguish your output from Python's output, and assumes that `>>> '
788at the start of a line is a prompt from Python. Similarly, the Emacs
789Shell mode code assumes that both `>>> ' and `... ' at the start of a
790line are Python prompts. Bad things can happen if you fool either
791mode.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000792
793Warning: If you do any editing *in* the process buffer *while* the
794buffer is accepting output from Python, do NOT attempt to `undo' the
795changes. Some of the output (nowhere near the parts you changed!) may
796be lost if you do. This appears to be an Emacs bug, an unfortunate
797interaction between undo and process filters; the same problem exists in
798non-Python process buffers using the default (Emacs-supplied) process
799filter."
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000800 ;; BAW - should undo be disabled in the python process buffer, if
801 ;; this bug still exists?
Barry Warsaw7ae77681994-12-12 20:38:05 +0000802 (interactive)
803 (if py-this-is-emacs-19-p
804 (progn
805 (require 'comint)
806 (switch-to-buffer-other-window
807 (make-comint "Python" py-python-command)))
808 (progn
809 (require 'shell)
810 (switch-to-buffer-other-window
Barry Warsaw9fbcc6a1996-01-23 22:52:02 +0000811 (apply (if (fboundp 'make-shell) 'make-shell 'make-comint)
Barry Warsaw6e98f331995-07-05 22:06:50 +0000812 "Python" py-python-command nil))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000813 (make-local-variable 'shell-prompt-pattern)
814 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
815 (set-process-filter (get-buffer-process (current-buffer))
816 'py-process-filter)
817 (set-syntax-table py-mode-syntax-table))
818
819(defun py-execute-region (start end)
820 "Send the region between START and END to a Python interpreter.
821If there is a *Python* process it is used.
822
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000823Hint: If you want to execute part of a Python file several times
824\(e.g., perhaps you're developing a function and want to flesh it out
825a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
826the region of interest, and send the code to a *Python* process via
827`\\[py-execute-buffer]' instead.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000828
829Following are subtleties to note when using a *Python* process:
830
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000831If a *Python* process is used, the region is copied into a temporary
832file (in directory `py-temp-directory'), and an `execfile' command is
833sent to Python naming that file. If you send regions faster than
834Python can execute them, `python-mode' will save them into distinct
835temp files, and execute the next one in the queue the next time it
836sees a `>>> ' prompt from Python. Each time this happens, the process
837buffer is popped into a window (if it's not already in some window) so
838you can see it, and a comment of the form
Barry Warsaw7ae77681994-12-12 20:38:05 +0000839
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000840 \t## working on region in file <name> ...
Barry Warsaw7ae77681994-12-12 20:38:05 +0000841
842is inserted at the end.
843
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000844Caution: No more than 26 regions can be pending at any given time.
845This limit is (indirectly) inherited from libc's mktemp(3).
846`python-mode' does not try to protect you from exceeding the limit.
847It's extremely unlikely that you'll get anywhere close to the limit in
848practice, unless you're trying to be a jerk <grin>.
Barry Warsaw7ae77681994-12-12 20:38:05 +0000849
850See the `\\[py-shell]' docs for additional warnings."
851 (interactive "r")
852 (or (< start end) (error "Region is empty"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000853 (let ((pyproc (get-process "Python"))
854 fname)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000855 (if (null pyproc)
856 (shell-command-on-region start end py-python-command)
857 ;; else feed it thru a temp file
858 (setq fname (py-make-temp-name))
859 (write-region start end fname nil 'no-msg)
860 (setq py-file-queue (append py-file-queue (list fname)))
861 (if (cdr py-file-queue)
862 (message "File %s queued for execution" fname)
863 ;; else
864 (py-execute-file pyproc fname)))))
865
866(defun py-execute-file (pyproc fname)
867 (py-append-to-process-buffer
868 pyproc
869 (format "## working on region in file %s ...\n" fname))
870 (process-send-string pyproc (format "execfile('%s')\n" fname)))
871
872(defun py-process-filter (pyproc string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000873 (let ((curbuf (current-buffer))
874 (pbuf (process-buffer pyproc))
875 (pmark (process-mark pyproc))
876 file-finished)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000877
878 ;; make sure we switch to a different buffer at least once. if we
879 ;; *don't* do this, then if the process buffer is in the selected
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000880 ;; window, and point is before the end, and lots of output is
881 ;; coming at a fast pace, then (a) simple cursor-movement commands
882 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
883 ;; to have a visible effect (the window just doesn't get updated,
884 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
885 ;; get all the process output (until the next python prompt).
Barry Warsaw7ae77681994-12-12 20:38:05 +0000886 ;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000887 ;; #b makes no sense to me at all. #a almost makes sense: unless
888 ;; we actually change buffers, set_buffer_internal in buffer.c
889 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
890 ;; seems to make the Emacs command loop reluctant to update the
891 ;; display. Perhaps the default process filter in process.c's
892 ;; read_process_output has update_mode_lines++ for a similar
893 ;; reason? beats me ...
894
895 ;; BAW - we want to check to see if this still applies
Barry Warsaw7ae77681994-12-12 20:38:05 +0000896 (if (eq curbuf pbuf) ; mysterious ugly hack
897 (set-buffer (get-buffer-create "*scratch*")))
898
899 (set-buffer pbuf)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000900 (let* ((start (point))
901 (goback (< start pmark))
Barry Warsawe64bfee1995-07-05 22:27:23 +0000902 (goend (and (not goback) (= start (point-max))))
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000903 (buffer-read-only nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000904 (goto-char pmark)
905 (insert string)
906 (move-marker pmark (point))
907 (setq file-finished
908 (and py-file-queue
909 (equal ">>> "
910 (buffer-substring
911 (prog2 (beginning-of-line) (point)
912 (goto-char pmark))
913 (point)))))
914 (if goback (goto-char start)
915 ;; else
916 (if py-scroll-process-buffer
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000917 (let* ((pop-up-windows t)
918 (pwin (display-buffer pbuf)))
Barry Warsawe64bfee1995-07-05 22:27:23 +0000919 (set-window-point pwin (point)))))
920 (set-buffer curbuf)
921 (if file-finished
922 (progn
923 (py-delete-file-silently (car py-file-queue))
924 (setq py-file-queue (cdr py-file-queue))
925 (if py-file-queue
926 (py-execute-file pyproc (car py-file-queue)))))
927 (and goend
928 (progn (set-buffer pbuf)
929 (goto-char (point-max))))
930 )))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000931
932(defun py-execute-buffer ()
933 "Send the contents of the buffer to a Python interpreter.
934If there is a *Python* process buffer it is used. If a clipping
935restriction is in effect, only the accessible portion of the buffer is
936sent. A trailing newline will be supplied if needed.
937
938See the `\\[py-execute-region]' docs for an account of some subtleties."
939 (interactive)
940 (py-execute-region (point-min) (point-max)))
941
942
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000943
944;; Functions for Python style indentation
Barry Warsaw03970731996-07-03 23:12:52 +0000945(defun py-delete-char (count)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000946 "Reduce indentation or delete character.
947If point is at the leftmost column, deletes the preceding newline.
948
949Else if point is at the leftmost non-blank character of a line that is
950neither a continuation line nor a non-indenting comment line, or if
951point is at the end of a blank line, reduces the indentation to match
952that of the line that opened the current block of code. The line that
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000953opened the block is displayed in the echo area to help you keep track
Barry Warsaw03970731996-07-03 23:12:52 +0000954of where you are. With numeric count, outdents that many blocks (but
955not past column zero).
Barry Warsaw7ae77681994-12-12 20:38:05 +0000956
957Else the preceding character is deleted, converting a tab to spaces if
Barry Warsaw03970731996-07-03 23:12:52 +0000958needed so that only a single column position is deleted. Numeric
959argument delets that many characters."
960 (interactive "*p")
Barry Warsaw7ae77681994-12-12 20:38:05 +0000961 (if (or (/= (current-indentation) (current-column))
962 (bolp)
963 (py-continuation-line-p)
Barry Warsaw6e527d21996-08-01 15:57:48 +0000964 (not py-honor-comment-indentation)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000965 (looking-at "#[^ \t\n]")) ; non-indenting #
Barry Warsaw03970731996-07-03 23:12:52 +0000966 (backward-delete-char-untabify count)
Barry Warsaw7ae77681994-12-12 20:38:05 +0000967 ;; else indent the same as the colon line that opened the block
968
969 ;; force non-blank so py-goto-block-up doesn't ignore it
970 (insert-char ?* 1)
971 (backward-char)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000972 (let ((base-indent 0) ; indentation of base line
973 (base-text "") ; and text of base line
974 (base-found-p nil))
Barry Warsaw03970731996-07-03 23:12:52 +0000975 (save-excursion
976 (while (< 0 count)
977 (condition-case nil ; in case no enclosing block
978 (progn
979 (py-goto-block-up 'no-mark)
980 (setq base-indent (current-indentation)
981 base-text (py-suck-up-leading-text)
982 base-found-p t))
983 (error nil))
984 (setq count (1- count))))
Barry Warsaw7ae77681994-12-12 20:38:05 +0000985 (delete-char 1) ; toss the dummy character
986 (delete-horizontal-space)
987 (indent-to base-indent)
988 (if base-found-p
989 (message "Closes block: %s" base-text)))))
990
Barry Warsawfc8a01f1995-03-09 16:07:29 +0000991;; required for pending-del and delsel modes
992(put 'py-delete-char 'delete-selection 'supersede)
993(put 'py-delete-char 'pending-delete 'supersede)
994
Barry Warsaw7ae77681994-12-12 20:38:05 +0000995(defun py-indent-line ()
996 "Fix the indentation of the current line according to Python rules."
997 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +0000998 (let* ((ci (current-indentation))
999 (move-to-indentation-p (<= (current-column) ci))
Barry Warsawb86bbad1995-03-14 15:56:35 +00001000 (need (py-compute-indentation)))
Barry Warsaw4f009fb1995-03-14 20:53:08 +00001001 ;; see if we need to outdent
Barry Warsaw3874a3d1995-03-14 22:05:53 +00001002 (if (py-outdent-p)
Barry Warsaw0012c1e1995-03-14 16:32:55 +00001003 (setq need (- need py-indent-offset)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001004 (if (/= ci need)
1005 (save-excursion
1006 (beginning-of-line)
1007 (delete-horizontal-space)
1008 (indent-to need)))
1009 (if move-to-indentation-p (back-to-indentation))))
1010
1011(defun py-newline-and-indent ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001012 "Strives to act like the Emacs `newline-and-indent'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001013This is just `strives to' because correct indentation can't be computed
1014from scratch for Python code. In general, deletes the whitespace before
1015point, inserts a newline, and takes an educated guess as to how you want
1016the new line indented."
1017 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001018 (let ((ci (current-indentation)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001019 (if (< ci (current-column)) ; if point beyond indentation
1020 (newline-and-indent)
1021 ;; else try to act like newline-and-indent "normally" acts
1022 (beginning-of-line)
1023 (insert-char ?\n 1)
1024 (move-to-column ci))))
1025
1026(defun py-compute-indentation ()
1027 (save-excursion
Barry Warsaw095e9c61995-09-19 20:01:42 +00001028 (let ((pps (parse-partial-sexp (save-excursion
1029 (beginning-of-python-def-or-class)
1030 (point))
1031 (point))))
1032 (beginning-of-line)
1033 (cond
1034 ;; are we inside a string or comment?
1035 ((or (nth 3 pps) (nth 4 pps))
1036 (save-excursion
1037 (if (not py-align-multiline-strings-p) 0
1038 ;; skip back over blank & non-indenting comment lines
1039 ;; note: will skip a blank or non-indenting comment line
1040 ;; that happens to be a continuation line too
1041 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)" nil 'move)
1042 (back-to-indentation)
1043 (current-column))))
1044 ;; are we on a continuation line?
1045 ((py-continuation-line-p)
1046 (let ((startpos (point))
1047 (open-bracket-pos (py-nesting-level))
Barry Warsawce60bc71996-08-01 18:17:14 +00001048 endpos searching found state)
Barry Warsaw095e9c61995-09-19 20:01:42 +00001049 (if open-bracket-pos
1050 (progn
1051 ;; align with first item in list; else a normal
1052 ;; indent beyond the line with the open bracket
1053 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
1054 ;; is the first list item on the same line?
1055 (skip-chars-forward " \t")
1056 (if (null (memq (following-char) '(?\n ?# ?\\)))
1057 ; yes, so line up with it
1058 (current-column)
1059 ;; first list item on another line, or doesn't exist yet
1060 (forward-line 1)
1061 (while (and (< (point) startpos)
1062 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
1063 (forward-line 1))
1064 (if (< (point) startpos)
1065 ;; again mimic the first list item
1066 (current-indentation)
1067 ;; else they're about to enter the first item
1068 (goto-char open-bracket-pos)
1069 (+ (current-indentation) py-indent-offset))))
1070
1071 ;; else on backslash continuation line
1072 (forward-line -1)
1073 (if (py-continuation-line-p) ; on at least 3rd line in block
1074 (current-indentation) ; so just continue the pattern
1075 ;; else started on 2nd line in block, so indent more.
1076 ;; if base line is an assignment with a start on a RHS,
1077 ;; indent to 2 beyond the leftmost "="; else skip first
1078 ;; chunk of non-whitespace characters on base line, + 1 more
1079 ;; column
1080 (end-of-line)
1081 (setq endpos (point) searching t)
1082 (back-to-indentation)
1083 (setq startpos (point))
1084 ;; look at all "=" from left to right, stopping at first
1085 ;; one not nested in a list or string
1086 (while searching
1087 (skip-chars-forward "^=" endpos)
1088 (if (= (point) endpos)
1089 (setq searching nil)
1090 (forward-char 1)
1091 (setq state (parse-partial-sexp startpos (point)))
1092 (if (and (zerop (car state)) ; not in a bracket
1093 (null (nth 3 state))) ; & not in a string
1094 (progn
1095 (setq searching nil) ; done searching in any case
1096 (setq found
1097 (not (or
1098 (eq (following-char) ?=)
1099 (memq (char-after (- (point) 2))
1100 '(?< ?> ?!)))))))))
1101 (if (or (not found) ; not an assignment
1102 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
1103 (progn
1104 (goto-char startpos)
1105 (skip-chars-forward "^ \t\n")))
1106 (1+ (current-column))))))
1107
1108 ;; not on a continuation line
Barry Warsawa7891711996-08-01 15:53:09 +00001109 ((bobp) (current-indentation))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001110
Barry Warsawa7891711996-08-01 15:53:09 +00001111 ;; Dfn: "Indenting comment line". A line containing only a
1112 ;; comment, but which is treated like a statement for
1113 ;; indentation calculation purposes. Such lines are only
1114 ;; treated specially by the mode; they are not treated
1115 ;; specially by the Python interpreter.
1116
1117 ;; The rules for indenting comment lines are a line where:
1118 ;; - the first non-whitespace character is `#', and
1119 ;; - the character following the `#' is whitespace, and
1120 ;; - the line is outdented with respect to (i.e. to the left
1121 ;; of) the indentation of the preceding non-blank line.
1122
1123 ;; The first non-blank line following an indenting comment
1124 ;; line is given the same amount of indentation as the
1125 ;; indenting comment line.
1126
1127 ;; All other comment-only lines are ignored for indentation
1128 ;; purposes.
1129
1130 ;; Are we looking at a comment-only line which is *not* an
1131 ;; indenting comment line? If so, we assume that its been
1132 ;; placed at the desired indentation, so leave it alone.
1133 ;; Indenting comment lines are aligned as statements down
1134 ;; below.
1135 ((and (looking-at "[ \t]*#[^ \t\n]")
1136 ;; NOTE: this test will not be performed in older Emacsen
1137 (fboundp 'forward-comment)
1138 (<= (current-indentation)
1139 (save-excursion
1140 (forward-comment (- (point-max)))
1141 (current-indentation))))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001142 (current-indentation))
1143
1144 ;; else indentation based on that of the statement that
1145 ;; precedes us; use the first line of that statement to
1146 ;; establish the base, in case the user forced a non-std
1147 ;; indentation for the continuation lines (if any)
1148 (t
Barry Warsawc01c5c81995-09-14 18:49:11 +00001149 ;; skip back over blank & non-indenting comment lines note:
1150 ;; will skip a blank or non-indenting comment line that
Barry Warsawfd0fb381996-03-04 17:15:40 +00001151 ;; happens to be a continuation line too. use fast Emacs 19
1152 ;; function if it's there.
Barry Warsaw6d627751996-03-06 18:41:38 +00001153 (if (and (eq py-honor-comment-indentation nil)
Barry Warsaw33d6ec01996-03-05 16:28:07 +00001154 (fboundp 'forward-comment))
Barry Warsawfd0fb381996-03-04 17:15:40 +00001155 (forward-comment (- (point-max)))
Barry Warsaw6d627751996-03-06 18:41:38 +00001156 (let (done)
1157 (while (not done)
1158 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
1159 nil 'move)
1160 (setq done (or (eq py-honor-comment-indentation t)
1161 (bobp)
1162 (/= (following-char) ?#)
1163 (not (zerop (current-column)))))
1164 )))
Barry Warsaw095e9c61995-09-19 20:01:42 +00001165 ;; if we landed inside a string, go to the beginning of that
1166 ;; string. this handles triple quoted, multi-line spanning
1167 ;; strings.
1168 (py-goto-initial-line)
Barry Warsawf831d811996-07-31 20:42:59 +00001169 (+ (current-indentation)
1170 (if (py-statement-opens-block-p)
1171 py-indent-offset
1172 (if (py-statement-closes-block-p)
1173 (- py-indent-offset)
1174 0)))
1175 )))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001176
1177(defun py-guess-indent-offset (&optional global)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001178 "Guess a good value for, and change, `py-indent-offset'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001179By default (without a prefix arg), makes a buffer-local copy of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001180`py-indent-offset' with the new value. This will not affect any other
Barry Warsaw7ae77681994-12-12 20:38:05 +00001181Python buffers. With a prefix arg, changes the global value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001182`py-indent-offset'. This affects all Python buffers (that don't have
Barry Warsaw7ae77681994-12-12 20:38:05 +00001183their own buffer-local copy), both those currently existing and those
1184created later in the Emacs session.
1185
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001186Some people use a different value for `py-indent-offset' than you use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001187There's no excuse for such foolishness, but sometimes you have to deal
1188with their ugly code anyway. This function examines the file and sets
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001189`py-indent-offset' to what it thinks it was when they created the
1190mess.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001191
1192Specifically, it searches forward from the statement containing point,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001193looking for a line that opens a block of code. `py-indent-offset' is
1194set to the difference in indentation between that line and the Python
Barry Warsaw7ae77681994-12-12 20:38:05 +00001195statement following it. If the search doesn't succeed going forward,
1196it's tried again going backward."
1197 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001198 (let (new-value
1199 (start (point))
1200 restart
1201 (found nil)
1202 colon-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001203 (py-goto-initial-line)
1204 (while (not (or found (eobp)))
1205 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1206 (progn
1207 (setq restart (point))
1208 (py-goto-initial-line)
1209 (if (py-statement-opens-block-p)
1210 (setq found t)
1211 (goto-char restart)))))
1212 (if found
1213 ()
1214 (goto-char start)
1215 (py-goto-initial-line)
1216 (while (not (or found (bobp)))
1217 (setq found
1218 (and
1219 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1220 (or (py-goto-initial-line) t) ; always true -- side effect
1221 (py-statement-opens-block-p)))))
1222 (setq colon-indent (current-indentation)
1223 found (and found (zerop (py-next-statement 1)))
1224 new-value (- (current-indentation) colon-indent))
1225 (goto-char start)
1226 (if found
1227 (progn
1228 (funcall (if global 'kill-local-variable 'make-local-variable)
1229 'py-indent-offset)
1230 (setq py-indent-offset new-value)
1231 (message "%s value of py-indent-offset set to %d"
1232 (if global "Global" "Local")
1233 py-indent-offset))
1234 (error "Sorry, couldn't guess a value for py-indent-offset"))))
1235
1236(defun py-shift-region (start end count)
1237 (save-excursion
1238 (goto-char end) (beginning-of-line) (setq end (point))
1239 (goto-char start) (beginning-of-line) (setq start (point))
1240 (indent-rigidly start end count)))
1241
1242(defun py-shift-region-left (start end &optional count)
1243 "Shift region of Python code to the left.
1244The lines from the line containing the start of the current region up
1245to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001246shifted to the left, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001247
1248If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001249many columns. With no active region, outdent only the current line.
1250You cannot outdent the region if any line is already at column zero."
1251 (interactive
1252 (let ((p (point))
1253 (m (mark))
1254 (arg current-prefix-arg))
1255 (if m
1256 (list (min p m) (max p m) arg)
1257 (list p (save-excursion (forward-line 1) (point)) arg))))
1258 ;; if any line is at column zero, don't shift the region
1259 (save-excursion
1260 (goto-char start)
1261 (while (< (point) end)
1262 (back-to-indentation)
Barry Warsaw71e315b1996-07-23 15:03:16 +00001263 (if (and (zerop (current-column))
1264 (not (looking-at "\\s *$")))
Barry Warsawdea4a291996-07-03 22:59:12 +00001265 (error "Region is at left edge."))
1266 (forward-line 1)))
1267 (py-shift-region start end (- (prefix-numeric-value
1268 (or count py-indent-offset))))
1269 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001270
1271(defun py-shift-region-right (start end &optional count)
1272 "Shift region of Python code to the right.
1273The lines from the line containing the start of the current region up
1274to (but not including) the line containing the end of the region are
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001275shifted to the right, by `py-indent-offset' columns.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001276
1277If a prefix argument is given, the region is instead shifted by that
Barry Warsawdea4a291996-07-03 22:59:12 +00001278many columns. With no active region, indent only the current line."
1279 (interactive
1280 (let ((p (point))
1281 (m (mark))
1282 (arg current-prefix-arg))
1283 (if m
1284 (list (min p m) (max p m) arg)
1285 (list p (save-excursion (forward-line 1) (point)) arg))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001286 (py-shift-region start end (prefix-numeric-value
Barry Warsawdea4a291996-07-03 22:59:12 +00001287 (or count py-indent-offset)))
1288 (py-keep-region-active))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001289
1290(defun py-indent-region (start end &optional indent-offset)
1291 "Reindent a region of Python code.
Barry Warsaw867a32a1996-03-07 18:30:26 +00001292
Barry Warsaw7ae77681994-12-12 20:38:05 +00001293The lines from the line containing the start of the current region up
1294to (but not including) the line containing the end of the region are
1295reindented. If the first line of the region has a non-whitespace
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001296character in the first column, the first line is left alone and the
1297rest of the region is reindented with respect to it. Else the entire
Barry Warsaw867a32a1996-03-07 18:30:26 +00001298region is reindented with respect to the (closest code or indenting
1299comment) statement immediately preceding the region.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001300
1301This is useful when code blocks are moved or yanked, when enclosing
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001302control structures are introduced or removed, or to reformat code
1303using a new value for the indentation offset.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001304
1305If a numeric prefix argument is given, it will be used as the value of
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001306the indentation offset. Else the value of `py-indent-offset' will be
Barry Warsaw7ae77681994-12-12 20:38:05 +00001307used.
1308
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001309Warning: The region must be consistently indented before this function
Barry Warsaw7ae77681994-12-12 20:38:05 +00001310is called! This function does not compute proper indentation from
1311scratch (that's impossible in Python), it merely adjusts the existing
1312indentation to be correct in context.
1313
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001314Warning: This function really has no idea what to do with
1315non-indenting comment lines, and shifts them as if they were indenting
1316comment lines. Fixing this appears to require telepathy.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001317
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001318Special cases: whitespace is deleted from blank lines; continuation
1319lines are shifted by the same amount their initial line was shifted,
1320in order to preserve their relative indentation with respect to their
Barry Warsaw7ae77681994-12-12 20:38:05 +00001321initial line; and comment lines beginning in column 1 are ignored."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001322 (interactive "*r\nP") ; region; raw prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001323 (save-excursion
1324 (goto-char end) (beginning-of-line) (setq end (point-marker))
1325 (goto-char start) (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001326 (let ((py-indent-offset (prefix-numeric-value
1327 (or indent-offset py-indent-offset)))
1328 (indents '(-1)) ; stack of active indent levels
1329 (target-column 0) ; column to which to indent
1330 (base-shifted-by 0) ; amount last base line was shifted
1331 (indent-base (if (looking-at "[ \t\n]")
1332 (py-compute-indentation)
1333 0))
1334 ci)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001335 (while (< (point) end)
1336 (setq ci (current-indentation))
1337 ;; figure out appropriate target column
1338 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001339 ((or (eq (following-char) ?#) ; comment in column 1
1340 (looking-at "[ \t]*$")) ; entirely blank
1341 (setq target-column 0))
1342 ((py-continuation-line-p) ; shift relative to base line
1343 (setq target-column (+ ci base-shifted-by)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001344 (t ; new base line
1345 (if (> ci (car indents)) ; going deeper; push it
1346 (setq indents (cons ci indents))
1347 ;; else we should have seen this indent before
1348 (setq indents (memq ci indents)) ; pop deeper indents
1349 (if (null indents)
1350 (error "Bad indentation in region, at line %d"
1351 (save-restriction
1352 (widen)
1353 (1+ (count-lines 1 (point)))))))
1354 (setq target-column (+ indent-base
1355 (* py-indent-offset
1356 (- (length indents) 2))))
1357 (setq base-shifted-by (- target-column ci))))
1358 ;; shift as needed
1359 (if (/= ci target-column)
1360 (progn
1361 (delete-horizontal-space)
1362 (indent-to target-column)))
1363 (forward-line 1))))
1364 (set-marker end nil))
1365
Barry Warsawa7891711996-08-01 15:53:09 +00001366(defun py-comment-region (beg end &optional arg)
1367 "Like `comment-region' but uses double hash (`#') comment starter."
1368 (interactive "r\nP")
1369 (let ((comment-start "## "))
1370 (comment-region beg end arg)))
1371
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001372
1373;; Functions for moving point
Barry Warsaw7ae77681994-12-12 20:38:05 +00001374(defun py-previous-statement (count)
1375 "Go to the start of previous Python statement.
1376If the statement at point is the i'th Python statement, goes to the
1377start of statement i-COUNT. If there is no such statement, goes to the
1378first statement. Returns count of statements left to move.
1379`Statements' do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001380 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001381 (if (< count 0) (py-next-statement (- count))
1382 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001383 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001384 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001385 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001386 (> count 0)
1387 (zerop (forward-line -1))
1388 (py-goto-statement-at-or-above))
1389 (setq count (1- count)))
1390 (if (> count 0) (goto-char start)))
1391 count))
1392
1393(defun py-next-statement (count)
1394 "Go to the start of next Python statement.
1395If the statement at point is the i'th Python statement, goes to the
1396start of statement i+COUNT. If there is no such statement, goes to the
1397last statement. Returns count of statements left to move. `Statements'
1398do not include blank, comment, or continuation lines."
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001399 (interactive "p") ; numeric prefix arg
Barry Warsaw7ae77681994-12-12 20:38:05 +00001400 (if (< count 0) (py-previous-statement (- count))
1401 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001402 (let (start)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001403 (while (and
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001404 (setq start (point)) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00001405 (> count 0)
1406 (py-goto-statement-below))
1407 (setq count (1- count)))
1408 (if (> count 0) (goto-char start)))
1409 count))
1410
1411(defun py-goto-block-up (&optional nomark)
1412 "Move up to start of current block.
1413Go to the statement that starts the smallest enclosing block; roughly
1414speaking, this will be the closest preceding statement that ends with a
1415colon and is indented less than the statement you started on. If
1416successful, also sets the mark to the starting point.
1417
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001418`\\[py-mark-block]' can be used afterward to mark the whole code
1419block, if desired.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001420
1421If called from a program, the mark will not be set if optional argument
1422NOMARK is not nil."
1423 (interactive)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001424 (let ((start (point))
1425 (found nil)
1426 initial-indent)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001427 (py-goto-initial-line)
1428 ;; if on blank or non-indenting comment line, use the preceding stmt
1429 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1430 (progn
1431 (py-goto-statement-at-or-above)
1432 (setq found (py-statement-opens-block-p))))
1433 ;; search back for colon line indented less
1434 (setq initial-indent (current-indentation))
1435 (if (zerop initial-indent)
1436 ;; force fast exit
1437 (goto-char (point-min)))
1438 (while (not (or found (bobp)))
1439 (setq found
1440 (and
1441 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
1442 (or (py-goto-initial-line) t) ; always true -- side effect
1443 (< (current-indentation) initial-indent)
1444 (py-statement-opens-block-p))))
1445 (if found
1446 (progn
1447 (or nomark (push-mark start))
1448 (back-to-indentation))
1449 (goto-char start)
1450 (error "Enclosing block not found"))))
1451
1452(defun beginning-of-python-def-or-class (&optional class)
1453 "Move point to start of def (or class, with prefix arg).
1454
1455Searches back for the closest preceding `def'. If you supply a prefix
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001456arg, looks for a `class' instead. The docs assume the `def' case;
1457just substitute `class' for `def' for the other case.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001458
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001459If point is in a def statement already, and after the `d', simply
1460moves point to the start of the statement.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001461
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001462Else (point is not in a def statement, or at or before the `d' of a
1463def statement), searches for the closest preceding def statement, and
1464leaves point at its start. If no such statement can be found, leaves
1465point at the start of the buffer.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001466
1467Returns t iff a def statement is found by these rules.
1468
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001469Note that doing this command repeatedly will take you closer to the
1470start of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001471
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001472If you want to mark the current def/class, see
1473`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001474 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001475 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1476 (start-of-line (progn (beginning-of-line) (point)))
1477 (start-of-stmt (progn (py-goto-initial-line) (point))))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001478 (if (or (/= start-of-stmt start-of-line)
1479 (not at-or-before-p))
1480 (end-of-line)) ; OK to match on this line
1481 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001482 nil 'move)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001483
1484(defun end-of-python-def-or-class (&optional class)
1485 "Move point beyond end of def (or class, with prefix arg) body.
1486
1487By default, looks for an appropriate `def'. If you supply a prefix arg,
1488looks for a `class' instead. The docs assume the `def' case; just
1489substitute `class' for `def' for the other case.
1490
1491If point is in a def statement already, this is the def we use.
1492
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001493Else if the def found by `\\[beginning-of-python-def-or-class]'
1494contains the statement you started on, that's the def we use.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001495
1496Else we search forward for the closest following def, and use that.
1497
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001498If a def can be found by these rules, point is moved to the start of
1499the line immediately following the def block, and the position of the
1500start of the def is returned.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001501
1502Else point is moved to the end of the buffer, and nil is returned.
1503
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001504Note that doing this command repeatedly will take you closer to the
1505end of the buffer each time.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001506
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001507If you want to mark the current def/class, see
1508`\\[mark-python-def-or-class]'."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001509 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001510 (let ((start (progn (py-goto-initial-line) (point)))
1511 (which (if class "class" "def"))
1512 (state 'not-found))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001513 ;; move point to start of appropriate def/class
1514 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1515 (setq state 'at-beginning)
1516 ;; else see if beginning-of-python-def-or-class hits container
1517 (if (and (beginning-of-python-def-or-class class)
1518 (progn (py-goto-beyond-block)
1519 (> (point) start)))
1520 (setq state 'at-end)
1521 ;; else search forward
1522 (goto-char start)
1523 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1524 (progn (setq state 'at-beginning)
1525 (beginning-of-line)))))
1526 (cond
1527 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1528 ((eq state 'at-end) t)
1529 ((eq state 'not-found) nil)
1530 (t (error "internal error in end-of-python-def-or-class")))))
1531
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001532
1533;; Functions for marking regions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001534(defun py-mark-block (&optional extend just-move)
1535 "Mark following block of lines. With prefix arg, mark structure.
1536Easier to use than explain. It sets the region to an `interesting'
1537block of succeeding lines. If point is on a blank line, it goes down to
1538the next non-blank line. That will be the start of the region. The end
1539of the region depends on the kind of line at the start:
1540
1541 - If a comment, the region will include all succeeding comment lines up
1542 to (but not including) the next non-comment line (if any).
1543
1544 - Else if a prefix arg is given, and the line begins one of these
1545 structures:
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001546
1547 if elif else try except finally for while def class
1548
Barry Warsaw7ae77681994-12-12 20:38:05 +00001549 the region will be set to the body of the structure, including
1550 following blocks that `belong' to it, but excluding trailing blank
1551 and comment lines. E.g., if on a `try' statement, the `try' block
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001552 and all (if any) of the following `except' and `finally' blocks
1553 that belong to the `try' structure will be in the region. Ditto
1554 for if/elif/else, for/else and while/else structures, and (a bit
1555 degenerate, since they're always one-block structures) def and
1556 class blocks.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001557
1558 - Else if no prefix argument is given, and the line begins a Python
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001559 block (see list above), and the block is not a `one-liner' (i.e.,
1560 the statement ends with a colon, not with code), the region will
1561 include all succeeding lines up to (but not including) the next
1562 code statement (if any) that's indented no more than the starting
1563 line, except that trailing blank and comment lines are excluded.
1564 E.g., if the starting line begins a multi-statement `def'
1565 structure, the region will be set to the full function definition,
1566 but without any trailing `noise' lines.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001567
1568 - Else the region will include all succeeding lines up to (but not
1569 including) the next blank line, or code or indenting-comment line
1570 indented strictly less than the starting line. Trailing indenting
1571 comment lines are included in this case, but not trailing blank
1572 lines.
1573
1574A msg identifying the location of the mark is displayed in the echo
1575area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1576
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001577If called from a program, optional argument EXTEND plays the role of
1578the prefix arg, and if optional argument JUST-MOVE is not nil, just
1579moves to the end of the block (& does not set mark or display a msg)."
Barry Warsaw7ae77681994-12-12 20:38:05 +00001580 (interactive "P") ; raw prefix arg
1581 (py-goto-initial-line)
1582 ;; skip over blank lines
1583 (while (and
1584 (looking-at "[ \t]*$") ; while blank line
1585 (not (eobp))) ; & somewhere to go
1586 (forward-line 1))
1587 (if (eobp)
1588 (error "Hit end of buffer without finding a non-blank stmt"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001589 (let ((initial-pos (point))
1590 (initial-indent (current-indentation))
1591 last-pos ; position of last stmt in region
1592 (followers
1593 '((if elif else) (elif elif else) (else)
1594 (try except finally) (except except) (finally)
1595 (for else) (while else)
1596 (def) (class) ) )
1597 first-symbol next-symbol)
Barry Warsaw7ae77681994-12-12 20:38:05 +00001598
1599 (cond
1600 ;; if comment line, suck up the following comment lines
1601 ((looking-at "[ \t]*#")
1602 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
1603 (re-search-backward "^[ \t]*#") ; and back to last comment in block
1604 (setq last-pos (point)))
1605
1606 ;; else if line is a block line and EXTEND given, suck up
1607 ;; the whole structure
1608 ((and extend
1609 (setq first-symbol (py-suck-up-first-keyword) )
1610 (assq first-symbol followers))
1611 (while (and
1612 (or (py-goto-beyond-block) t) ; side effect
1613 (forward-line -1) ; side effect
1614 (setq last-pos (point)) ; side effect
1615 (py-goto-statement-below)
1616 (= (current-indentation) initial-indent)
1617 (setq next-symbol (py-suck-up-first-keyword))
1618 (memq next-symbol (cdr (assq first-symbol followers))))
1619 (setq first-symbol next-symbol)))
1620
1621 ;; else if line *opens* a block, search for next stmt indented <=
1622 ((py-statement-opens-block-p)
1623 (while (and
1624 (setq last-pos (point)) ; always true -- side effect
1625 (py-goto-statement-below)
1626 (> (current-indentation) initial-indent))
1627 nil))
1628
1629 ;; else plain code line; stop at next blank line, or stmt or
1630 ;; indenting comment line indented <
1631 (t
1632 (while (and
1633 (setq last-pos (point)) ; always true -- side effect
1634 (or (py-goto-beyond-final-line) t)
1635 (not (looking-at "[ \t]*$")) ; stop at blank line
1636 (or
1637 (>= (current-indentation) initial-indent)
1638 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
1639 nil)))
1640
1641 ;; skip to end of last stmt
1642 (goto-char last-pos)
1643 (py-goto-beyond-final-line)
1644
1645 ;; set mark & display
1646 (if just-move
1647 () ; just return
1648 (push-mark (point) 'no-msg)
1649 (forward-line -1)
1650 (message "Mark set after: %s" (py-suck-up-leading-text))
1651 (goto-char initial-pos))))
1652
1653(defun mark-python-def-or-class (&optional class)
1654 "Set region to body of def (or class, with prefix arg) enclosing point.
1655Pushes the current mark, then point, on the mark ring (all language
1656modes do this, but although it's handy it's never documented ...).
1657
1658In most Emacs language modes, this function bears at least a
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001659hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
1660`\\[beginning-of-python-def-or-class]'.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001661
1662And in earlier versions of Python mode, all 3 were tightly connected.
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001663Turned out that was more confusing than useful: the `goto start' and
1664`goto end' commands are usually used to search through a file, and
1665people expect them to act a lot like `search backward' and `search
1666forward' string-search commands. But because Python `def' and `class'
1667can nest to arbitrary levels, finding the smallest def containing
1668point cannot be done via a simple backward search: the def containing
1669point may not be the closest preceding def, or even the closest
1670preceding def that's indented less. The fancy algorithm required is
1671appropriate for the usual uses of this `mark' command, but not for the
1672`goto' variations.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001673
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001674So the def marked by this command may not be the one either of the
1675`goto' commands find: If point is on a blank or non-indenting comment
1676line, moves back to start of the closest preceding code statement or
1677indenting comment line. If this is a `def' statement, that's the def
1678we use. Else searches for the smallest enclosing `def' block and uses
1679that. Else signals an error.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001680
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001681When an enclosing def is found: The mark is left immediately beyond
1682the last line of the def block. Point is left at the start of the
1683def, except that: if the def is preceded by a number of comment lines
1684followed by (at most) one optional blank line, point is left at the
1685start of the comments; else if the def is preceded by a blank line,
1686point is left at its start.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001687
1688The intent is to mark the containing def/class and its associated
1689documentation, to make moving and duplicating functions and classes
1690pleasant."
1691 (interactive "P") ; raw prefix arg
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001692 (let ((start (point))
1693 (which (if class "class" "def")))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001694 (push-mark start)
1695 (if (not (py-go-up-tree-to-keyword which))
1696 (progn (goto-char start)
1697 (error "Enclosing %s not found" which))
1698 ;; else enclosing def/class found
1699 (setq start (point))
1700 (py-goto-beyond-block)
1701 (push-mark (point))
1702 (goto-char start)
1703 (if (zerop (forward-line -1)) ; if there is a preceding line
1704 (progn
1705 (if (looking-at "[ \t]*$") ; it's blank
1706 (setq start (point)) ; so reset start point
1707 (goto-char start)) ; else try again
1708 (if (zerop (forward-line -1))
1709 (if (looking-at "[ \t]*#") ; a comment
1710 ;; look back for non-comment line
1711 ;; tricky: note that the regexp matches a blank
1712 ;; line, cuz \n is in the 2nd character class
1713 (and
1714 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
1715 (forward-line 1))
1716 ;; no comment, so go back
1717 (goto-char start))))))))
1718
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00001719;; ripped from cc-mode
1720(defun py-forward-into-nomenclature (&optional arg)
1721 "Move forward to end of a nomenclature section or word.
1722With arg, to it arg times.
1723
1724A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
1725 (interactive "p")
1726 (let ((case-fold-search nil))
1727 (if (> arg 0)
Barry Warsawc846f461996-07-25 18:53:17 +00001728 (re-search-forward "\\W*\\([A-Z_]*[a-z0-9]*\\)" (point-max) t arg)
Barry Warsaw9e5a9c81996-07-24 18:26:53 +00001729 (while (and (< arg 0)
1730 (re-search-backward
1731 "\\(\\(\\W\\|[a-z0-9]\\)[A-Z]+\\|\\W\\w+\\)"
1732 (point-min) 0))
1733 (forward-char 1)
1734 (setq arg (1+ arg)))))
1735 (py-keep-region-active))
1736
1737(defun py-backward-into-nomenclature (&optional arg)
1738 "Move backward to beginning of a nomenclature section or word.
1739With optional ARG, move that many times. If ARG is negative, move
1740forward.
1741
1742A `nomenclature' is a fancy way of saying AWordWithMixedCaseNotUnderscores."
1743 (interactive "p")
1744 (py-forward-into-nomenclature (- arg))
1745 (py-keep-region-active))
1746
1747
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001748
1749;; Documentation functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00001750
1751;; dump the long form of the mode blurb; does the usual doc escapes,
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001752;; plus lines of the form ^[vc]:name$ to suck variable & command docs
1753;; out of the right places, along with the keys they're on & current
1754;; values
Barry Warsaw7ae77681994-12-12 20:38:05 +00001755(defun py-dump-help-string (str)
1756 (with-output-to-temp-buffer "*Help*"
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001757 (let ((locals (buffer-local-variables))
1758 funckind funcname func funcdoc
1759 (start 0) mstart end
1760 keys )
Barry Warsaw7ae77681994-12-12 20:38:05 +00001761 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
1762 (setq mstart (match-beginning 0) end (match-end 0)
1763 funckind (substring str (match-beginning 1) (match-end 1))
1764 funcname (substring str (match-beginning 2) (match-end 2))
1765 func (intern funcname))
1766 (princ (substitute-command-keys (substring str start mstart)))
1767 (cond
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001768 ((equal funckind "c") ; command
1769 (setq funcdoc (documentation func)
1770 keys (concat
1771 "Key(s): "
1772 (mapconcat 'key-description
1773 (where-is-internal func py-mode-map)
1774 ", "))))
1775 ((equal funckind "v") ; variable
1776 (setq funcdoc (substitute-command-keys
1777 (get func 'variable-documentation))
1778 keys (if (assq func locals)
1779 (concat
1780 "Local/Global values: "
1781 (prin1-to-string (symbol-value func))
1782 " / "
1783 (prin1-to-string (default-value func)))
1784 (concat
1785 "Value: "
1786 (prin1-to-string (symbol-value func))))))
1787 (t ; unexpected
1788 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00001789 (princ (format "\n-> %s:\t%s\t%s\n\n"
1790 (if (equal funckind "c") "Command" "Variable")
1791 funcname keys))
1792 (princ funcdoc)
1793 (terpri)
1794 (setq start end))
1795 (princ (substitute-command-keys (substring str start))))
1796 (print-help-return-message)))
1797
1798(defun py-describe-mode ()
1799 "Dump long form of Python-mode docs."
1800 (interactive)
1801 (py-dump-help-string "Major mode for editing Python files.
1802Knows about Python indentation, tokens, comments and continuation lines.
1803Paragraphs are separated by blank lines only.
1804
1805Major sections below begin with the string `@'; specific function and
1806variable docs begin with `->'.
1807
1808@EXECUTING PYTHON CODE
1809
1810\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
1811\\[py-execute-region]\tsends the current region
1812\\[py-shell]\tstarts a Python interpreter window; this will be used by
1813\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
1814%c:py-execute-buffer
1815%c:py-execute-region
1816%c:py-shell
1817
1818@VARIABLES
1819
1820py-indent-offset\tindentation increment
Barry Warsaw42f707f1996-07-29 21:05:05 +00001821py-block-comment-prefix\tcomment string used by comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00001822
1823py-python-command\tshell command to invoke Python interpreter
1824py-scroll-process-buffer\talways scroll Python process buffer
1825py-temp-directory\tdirectory used for temp files (if needed)
1826
1827py-beep-if-tab-change\tring the bell if tab-width is changed
1828%v:py-indent-offset
1829%v:py-block-comment-prefix
1830%v:py-python-command
1831%v:py-scroll-process-buffer
1832%v:py-temp-directory
1833%v:py-beep-if-tab-change
1834
1835@KINDS OF LINES
1836
1837Each physical line in the file is either a `continuation line' (the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001838preceding line ends with a backslash that's not part of a comment, or
1839the paren/bracket/brace nesting level at the start of the line is
1840non-zero, or both) or an `initial line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001841
1842An initial line is in turn a `blank line' (contains nothing except
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001843possibly blanks or tabs), a `comment line' (leftmost non-blank
1844character is `#'), or a `code line' (everything else).
Barry Warsaw7ae77681994-12-12 20:38:05 +00001845
1846Comment Lines
1847
1848Although all comment lines are treated alike by Python, Python mode
1849recognizes two kinds that act differently with respect to indentation.
1850
1851An `indenting comment line' is a comment line with a blank, tab or
1852nothing after the initial `#'. The indentation commands (see below)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001853treat these exactly as if they were code lines: a line following an
Barry Warsaw7ae77681994-12-12 20:38:05 +00001854indenting comment line will be indented like the comment line. All
1855other comment lines (those with a non-whitespace character immediately
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001856following the initial `#') are `non-indenting comment lines', and
1857their indentation is ignored by the indentation commands.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001858
1859Indenting comment lines are by far the usual case, and should be used
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001860whenever possible. Non-indenting comment lines are useful in cases
1861like these:
Barry Warsaw7ae77681994-12-12 20:38:05 +00001862
1863\ta = b # a very wordy single-line comment that ends up being
1864\t #... continued onto another line
1865
1866\tif a == b:
1867##\t\tprint 'panic!' # old code we've `commented out'
1868\t\treturn a
1869
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001870Since the `#...' and `##' comment lines have a non-whitespace
1871character following the initial `#', Python mode ignores them when
1872computing the proper indentation for the next line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001873
1874Continuation Lines and Statements
1875
1876The Python-mode commands generally work on statements instead of on
1877individual lines, where a `statement' is a comment or blank line, or a
1878code line and all of its following continuation lines (if any)
1879considered as a single logical unit. The commands in this mode
1880generally (when it makes sense) automatically move to the start of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00001881statement containing point, even if point happens to be in the middle
1882of some continuation line.
Barry Warsaw7ae77681994-12-12 20:38:05 +00001883
1884
1885@INDENTATION
1886
1887Primarily for entering new code:
1888\t\\[indent-for-tab-command]\t indent line appropriately
1889\t\\[py-newline-and-indent]\t insert newline, then indent
1890\t\\[py-delete-char]\t reduce indentation, or delete single character
1891
1892Primarily for reindenting existing code:
1893\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
1894\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
1895
1896\t\\[py-indent-region]\t reindent region to match its context
1897\t\\[py-shift-region-left]\t shift region left by py-indent-offset
1898\t\\[py-shift-region-right]\t shift region right by py-indent-offset
1899
1900Unlike most programming languages, Python uses indentation, and only
1901indentation, to specify block structure. Hence the indentation supplied
1902automatically by Python-mode is just an educated guess: only you know
1903the block structure you intend, so only you can supply correct
1904indentation.
1905
1906The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
1907the indentation of preceding statements. E.g., assuming
1908py-indent-offset is 4, after you enter
1909\tif a > 0: \\[py-newline-and-indent]
1910the cursor will be moved to the position of the `_' (_ is not a
1911character in the file, it's just used here to indicate the location of
1912the cursor):
1913\tif a > 0:
1914\t _
1915If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
1916to
1917\tif a > 0:
1918\t c = d
1919\t _
1920Python-mode cannot know whether that's what you intended, or whether
1921\tif a > 0:
1922\t c = d
1923\t_
1924was your intent. In general, Python-mode either reproduces the
1925indentation of the (closest code or indenting-comment) preceding
1926statement, or adds an extra py-indent-offset blanks if the preceding
1927statement has `:' as its last significant (non-whitespace and non-
1928comment) character. If the suggested indentation is too much, use
1929\\[py-delete-char] to reduce it.
1930
1931Continuation lines are given extra indentation. If you don't like the
1932suggested indentation, change it to something you do like, and Python-
1933mode will strive to indent later lines of the statement in the same way.
1934
1935If a line is a continuation line by virtue of being in an unclosed
1936paren/bracket/brace structure (`list', for short), the suggested
1937indentation depends on whether the current line contains the first item
1938in the list. If it does, it's indented py-indent-offset columns beyond
1939the indentation of the line containing the open bracket. If you don't
1940like that, change it by hand. The remaining items in the list will mimic
1941whatever indentation you give to the first item.
1942
1943If a line is a continuation line because the line preceding it ends with
1944a backslash, the third and following lines of the statement inherit their
1945indentation from the line preceding them. The indentation of the second
1946line in the statement depends on the form of the first (base) line: if
1947the base line is an assignment statement with anything more interesting
1948than the backslash following the leftmost assigning `=', the second line
1949is indented two columns beyond that `='. Else it's indented to two
1950columns beyond the leftmost solid chunk of non-whitespace characters on
1951the base line.
1952
1953Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
1954repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
1955structure you intend.
1956%c:indent-for-tab-command
1957%c:py-newline-and-indent
1958%c:py-delete-char
1959
1960
1961The next function may be handy when editing code you didn't write:
1962%c:py-guess-indent-offset
1963
1964
1965The remaining `indent' functions apply to a region of Python code. They
1966assume the block structure (equals indentation, in Python) of the region
1967is correct, and alter the indentation in various ways while preserving
1968the block structure:
1969%c:py-indent-region
1970%c:py-shift-region-left
1971%c:py-shift-region-right
1972
1973@MARKING & MANIPULATING REGIONS OF CODE
1974
1975\\[py-mark-block]\t mark block of lines
1976\\[mark-python-def-or-class]\t mark smallest enclosing def
1977\\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
Barry Warsaw42f707f1996-07-29 21:05:05 +00001978\\[comment-region]\t comment out region of code
1979\\[universal-argument] \\[comment-region]\t uncomment region of code
Barry Warsaw7ae77681994-12-12 20:38:05 +00001980%c:py-mark-block
1981%c:mark-python-def-or-class
Barry Warsaw42f707f1996-07-29 21:05:05 +00001982%c:comment-region
Barry Warsaw7ae77681994-12-12 20:38:05 +00001983
1984@MOVING POINT
1985
1986\\[py-previous-statement]\t move to statement preceding point
1987\\[py-next-statement]\t move to statement following point
1988\\[py-goto-block-up]\t move up to start of current block
1989\\[beginning-of-python-def-or-class]\t move to start of def
1990\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
1991\\[end-of-python-def-or-class]\t move to end of def
1992\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
1993
1994The first two move to one statement beyond the statement that contains
1995point. A numeric prefix argument tells them to move that many
1996statements instead. Blank lines, comment lines, and continuation lines
1997do not count as `statements' for these commands. So, e.g., you can go
1998to the first code statement in a file by entering
1999\t\\[beginning-of-buffer]\t to move to the top of the file
2000\t\\[py-next-statement]\t to skip over initial comments and blank lines
2001Or do `\\[py-previous-statement]' with a huge prefix argument.
2002%c:py-previous-statement
2003%c:py-next-statement
2004%c:py-goto-block-up
2005%c:beginning-of-python-def-or-class
2006%c:end-of-python-def-or-class
2007
2008@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
2009
2010`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
2011
2012`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
2013overall class and def structure of a module.
2014
2015`\\[back-to-indentation]' moves point to a line's first non-blank character.
2016
2017`\\[indent-relative]' is handy for creating odd indentation.
2018
2019@OTHER EMACS HINTS
2020
2021If you don't like the default value of a variable, change its value to
2022whatever you do like by putting a `setq' line in your .emacs file.
2023E.g., to set the indentation increment to 4, put this line in your
2024.emacs:
2025\t(setq py-indent-offset 4)
2026To see the value of a variable, do `\\[describe-variable]' and enter the variable
2027name at the prompt.
2028
2029When entering a key sequence like `C-c C-n', it is not necessary to
2030release the CONTROL key after doing the `C-c' part -- it suffices to
2031press the CONTROL key, press and release `c' (while still holding down
2032CONTROL), press and release `n' (while still holding down CONTROL), &
2033then release CONTROL.
2034
2035Entering Python mode calls with no arguments the value of the variable
2036`python-mode-hook', if that value exists and is not nil; for backward
2037compatibility it also tries `py-mode-hook'; see the `Hooks' section of
2038the Elisp manual for details.
2039
2040Obscure: When python-mode is first loaded, it looks for all bindings
2041to newline-and-indent in the global keymap, and shadows them with
2042local bindings to py-newline-and-indent."))
2043
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002044
2045;; Helper functions
Barry Warsaw7ae77681994-12-12 20:38:05 +00002046(defvar py-parse-state-re
2047 (concat
2048 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
2049 "\\|"
2050 "^[^ #\t\n]"))
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002051
Barry Warsaw7ae77681994-12-12 20:38:05 +00002052;; returns the parse state at point (see parse-partial-sexp docs)
2053(defun py-parse-state ()
2054 (save-excursion
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002055 (let ((here (point))
Barry Warsaw170ffa71996-07-31 20:57:22 +00002056 pps done ci)
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002057 (while (not done)
2058 ;; back up to the first preceding line (if any; else start of
2059 ;; buffer) that begins with a popular Python keyword, or a
2060 ;; non- whitespace and non-comment character. These are good
2061 ;; places to start parsing to see whether where we started is
2062 ;; at a non-zero nesting level. It may be slow for people who
2063 ;; write huge code blocks or huge lists ... tough beans.
2064 (re-search-backward py-parse-state-re nil 'move)
Barry Warsaw170ffa71996-07-31 20:57:22 +00002065 (setq ci (current-indentation))
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002066 (beginning-of-line)
2067 (save-excursion
2068 (setq pps (parse-partial-sexp (point) here)))
2069 ;; make sure we don't land inside a triple-quoted string
Barry Warsaw170ffa71996-07-31 20:57:22 +00002070 (setq done (or (zerop ci)
2071 (not (nth 3 pps))
2072 (bobp)))
2073 )
Barry Warsaw43ecf8e1996-04-06 00:00:19 +00002074 pps)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002075
2076;; if point is at a non-zero nesting level, returns the number of the
2077;; character that opens the smallest enclosing unclosed list; else
2078;; returns nil.
2079(defun py-nesting-level ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002080 (let ((status (py-parse-state)) )
Barry Warsaw7ae77681994-12-12 20:38:05 +00002081 (if (zerop (car status))
2082 nil ; not in a nest
2083 (car (cdr status))))) ; char# of open bracket
2084
2085;; t iff preceding line ends with backslash that's not in a comment
2086(defun py-backslash-continuation-line-p ()
2087 (save-excursion
2088 (beginning-of-line)
2089 (and
2090 ;; use a cheap test first to avoid the regexp if possible
2091 ;; use 'eq' because char-after may return nil
2092 (eq (char-after (- (point) 2)) ?\\ )
2093 ;; make sure; since eq test passed, there is a preceding line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002094 (forward-line -1) ; always true -- side effect
Barry Warsaw7ae77681994-12-12 20:38:05 +00002095 (looking-at py-continued-re))))
2096
2097;; t iff current line is a continuation line
2098(defun py-continuation-line-p ()
2099 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002100 (beginning-of-line)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002101 (or (py-backslash-continuation-line-p)
2102 (py-nesting-level))))
2103
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002104;; go to initial line of current statement; usually this is the line
2105;; we're on, but if we're on the 2nd or following lines of a
2106;; continuation block, we need to go up to the first line of the
2107;; block.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002108;;
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002109;; Tricky: We want to avoid quadratic-time behavior for long continued
2110;; blocks, whether of the backslash or open-bracket varieties, or a
2111;; mix of the two. The following manages to do that in the usual
2112;; cases.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002113(defun py-goto-initial-line ()
2114 (let ( open-bracket-pos )
2115 (while (py-continuation-line-p)
2116 (beginning-of-line)
2117 (if (py-backslash-continuation-line-p)
2118 (while (py-backslash-continuation-line-p)
2119 (forward-line -1))
2120 ;; else zip out of nested brackets/braces/parens
2121 (while (setq open-bracket-pos (py-nesting-level))
2122 (goto-char open-bracket-pos)))))
2123 (beginning-of-line))
2124
2125;; go to point right beyond final line of current statement; usually
2126;; this is the start of the next line, but if this is a multi-line
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002127;; statement we need to skip over the continuation lines. Tricky:
2128;; Again we need to be clever to avoid quadratic time behavior.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002129(defun py-goto-beyond-final-line ()
2130 (forward-line 1)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002131 (let (state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002132 (while (and (py-continuation-line-p)
2133 (not (eobp)))
2134 ;; skip over the backslash flavor
2135 (while (and (py-backslash-continuation-line-p)
2136 (not (eobp)))
2137 (forward-line 1))
2138 ;; if in nest, zip to the end of the nest
2139 (setq state (py-parse-state))
2140 (if (and (not (zerop (car state)))
2141 (not (eobp)))
2142 (progn
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002143 ;; BUG ALERT: I could swear, from reading the docs, that
Barry Warsaw7ae77681994-12-12 20:38:05 +00002144 ;; the 3rd argument should be plain 0
2145 (parse-partial-sexp (point) (point-max) (- 0 (car state))
2146 nil state)
2147 (forward-line 1))))))
2148
2149;; t iff statement opens a block == iff it ends with a colon that's
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002150;; not in a comment. point should be at the start of a statement
Barry Warsaw7ae77681994-12-12 20:38:05 +00002151(defun py-statement-opens-block-p ()
2152 (save-excursion
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002153 (let ((start (point))
2154 (finish (progn (py-goto-beyond-final-line) (1- (point))))
2155 (searching t)
2156 (answer nil)
2157 state)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002158 (goto-char start)
2159 (while searching
2160 ;; look for a colon with nothing after it except whitespace, and
2161 ;; maybe a comment
2162 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
2163 finish t)
2164 (if (eq (point) finish) ; note: no `else' clause; just
2165 ; keep searching if we're not at
2166 ; the end yet
2167 ;; sure looks like it opens a block -- but it might
2168 ;; be in a comment
2169 (progn
2170 (setq searching nil) ; search is done either way
2171 (setq state (parse-partial-sexp start
2172 (match-beginning 0)))
2173 (setq answer (not (nth 4 state)))))
2174 ;; search failed: couldn't find another interesting colon
2175 (setq searching nil)))
2176 answer)))
2177
Barry Warsawf831d811996-07-31 20:42:59 +00002178(defun py-statement-closes-block-p ()
2179 ;; true iff the current statement `closes' a block == the line
2180 ;; starts with `return', `raise', `break' or `continue'. doesn't
2181 ;; catch embedded statements
2182 (let ((here (point)))
2183 (back-to-indentation)
2184 (prog1
2185 (looking-at "\\(return\\|raise\\|break\\|continue\\)\\>")
2186 (goto-char here))))
2187
Barry Warsaw7ae77681994-12-12 20:38:05 +00002188;; go to point right beyond final line of block begun by the current
2189;; line. This is the same as where py-goto-beyond-final-line goes
2190;; unless we're on colon line, in which case we go to the end of the
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002191;; block. assumes point is at bolp
Barry Warsaw7ae77681994-12-12 20:38:05 +00002192(defun py-goto-beyond-block ()
2193 (if (py-statement-opens-block-p)
2194 (py-mark-block nil 'just-move)
2195 (py-goto-beyond-final-line)))
2196
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002197;; go to start of first statement (not blank or comment or
2198;; continuation line) at or preceding point. returns t if there is
2199;; one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002200(defun py-goto-statement-at-or-above ()
2201 (py-goto-initial-line)
2202 (if (looking-at py-blank-or-comment-re)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002203 ;; skip back over blank & comment lines
2204 ;; note: will skip a blank or comment line that happens to be
2205 ;; a continuation line too
2206 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
2207 (progn (py-goto-initial-line) t)
2208 nil)
Barry Warsaw7ae77681994-12-12 20:38:05 +00002209 t))
2210
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002211;; go to start of first statement (not blank or comment or
2212;; continuation line) following the statement containing point returns
2213;; t if there is one, else nil
Barry Warsaw7ae77681994-12-12 20:38:05 +00002214(defun py-goto-statement-below ()
2215 (beginning-of-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002216 (let ((start (point)))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002217 (py-goto-beyond-final-line)
2218 (while (and
2219 (looking-at py-blank-or-comment-re)
2220 (not (eobp)))
2221 (forward-line 1))
2222 (if (eobp)
2223 (progn (goto-char start) nil)
2224 t)))
2225
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002226;; go to start of statement, at or preceding point, starting with
2227;; keyword KEY. Skips blank lines and non-indenting comments upward
2228;; first. If that statement starts with KEY, done, else go back to
2229;; first enclosing block starting with KEY. If successful, leaves
2230;; point at the start of the KEY line & returns t. Else leaves point
2231;; at an undefined place & returns nil.
Barry Warsaw7ae77681994-12-12 20:38:05 +00002232(defun py-go-up-tree-to-keyword (key)
2233 ;; skip blanks and non-indenting #
2234 (py-goto-initial-line)
2235 (while (and
2236 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
2237 (zerop (forward-line -1))) ; go back
2238 nil)
2239 (py-goto-initial-line)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002240 (let* ((re (concat "[ \t]*" key "\\b"))
2241 (case-fold-search nil) ; let* so looking-at sees this
2242 (found (looking-at re))
2243 (dead nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002244 (while (not (or found dead))
2245 (condition-case nil ; in case no enclosing block
2246 (py-goto-block-up 'no-mark)
2247 (error (setq dead t)))
2248 (or dead (setq found (looking-at re))))
2249 (beginning-of-line)
2250 found))
2251
2252;; return string in buffer from start of indentation to end of line;
2253;; prefix "..." if leading whitespace was skipped
2254(defun py-suck-up-leading-text ()
2255 (save-excursion
2256 (back-to-indentation)
2257 (concat
2258 (if (bolp) "" "...")
2259 (buffer-substring (point) (progn (end-of-line) (point))))))
2260
2261;; assuming point at bolp, return first keyword ([a-z]+) on the line,
2262;; as a Lisp symbol; return nil if none
2263(defun py-suck-up-first-keyword ()
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002264 (let ((case-fold-search nil))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002265 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
2266 (intern (buffer-substring (match-beginning 1) (match-end 1)))
2267 nil)))
2268
2269(defun py-make-temp-name ()
2270 (make-temp-name
2271 (concat (file-name-as-directory py-temp-directory) "python")))
2272
2273(defun py-delete-file-silently (fname)
2274 (condition-case nil
2275 (delete-file fname)
2276 (error nil)))
2277
2278(defun py-kill-emacs-hook ()
2279 ;; delete our temp files
2280 (while py-file-queue
2281 (py-delete-file-silently (car py-file-queue))
2282 (setq py-file-queue (cdr py-file-queue)))
2283 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
2284 ;; run the hook we inherited, if any
2285 (and py-inherited-kill-emacs-hook
2286 (funcall py-inherited-kill-emacs-hook))))
2287
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002288;; make PROCESS's buffer visible, append STRING to it, and force
2289;; display; also make shell-mode believe the user typed this string,
2290;; so that kill-output-from-shell and show-output-from-shell work
2291;; "right"
Barry Warsaw7ae77681994-12-12 20:38:05 +00002292(defun py-append-to-process-buffer (process string)
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002293 (let ((cbuf (current-buffer))
2294 (pbuf (process-buffer process))
2295 (py-scroll-process-buffer t))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002296 (set-buffer pbuf)
2297 (goto-char (point-max))
2298 (move-marker (process-mark process) (point))
Barry Warsaw4dba7e21995-07-05 23:01:43 +00002299 (if (not (or py-this-is-emacs-19-p
2300 py-this-is-lucid-emacs-p))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002301 (move-marker last-input-start (point))) ; muck w/ shell-mode
2302 (funcall (process-filter process) process string)
Barry Warsaw4dba7e21995-07-05 23:01:43 +00002303 (if (not (or py-this-is-emacs-19-p
2304 py-this-is-lucid-emacs-p))
Barry Warsaw7ae77681994-12-12 20:38:05 +00002305 (move-marker last-input-end (point))) ; muck w/ shell-mode
2306 (set-buffer cbuf))
2307 (sit-for 0))
2308
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002309
2310
Barry Warsaw850437a1995-03-08 21:50:28 +00002311(defconst py-version "$Revision$"
2312 "`python-mode' version number.")
Barry Warsawfec75d61995-07-05 23:26:15 +00002313(defconst py-help-address "python-mode@python.org"
Barry Warsaw850437a1995-03-08 21:50:28 +00002314 "Address accepting submission of bug reports.")
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002315
Barry Warsaw850437a1995-03-08 21:50:28 +00002316(defun py-version ()
2317 "Echo the current version of `python-mode' in the minibuffer."
2318 (interactive)
2319 (message "Using `python-mode' version %s" py-version)
2320 (py-keep-region-active))
2321
2322;; only works under Emacs 19
2323;(eval-when-compile
2324; (require 'reporter))
2325
2326(defun py-submit-bug-report (enhancement-p)
2327 "Submit via mail a bug report on `python-mode'.
2328With \\[universal-argument] just submit an enhancement request."
2329 (interactive
2330 (list (not (y-or-n-p
2331 "Is this a bug report? (hit `n' to send other comments) "))))
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002332 (let ((reporter-prompt-for-summary-p (if enhancement-p
2333 "(Very) brief summary: "
2334 t)))
Barry Warsaw850437a1995-03-08 21:50:28 +00002335 (require 'reporter)
2336 (reporter-submit-bug-report
2337 py-help-address ;address
Barry Warsawb5e0ecb1995-03-14 18:32:54 +00002338 (concat "python-mode " py-version) ;pkgname
Barry Warsaw850437a1995-03-08 21:50:28 +00002339 ;; varlist
2340 (if enhancement-p nil
2341 '(py-python-command
2342 py-indent-offset
2343 py-block-comment-prefix
2344 py-scroll-process-buffer
2345 py-temp-directory
2346 py-beep-if-tab-change))
2347 nil ;pre-hooks
2348 nil ;post-hooks
2349 "Dear Barry,") ;salutation
2350 (if enhancement-p nil
2351 (set-mark (point))
2352 (insert
2353"Please replace this text with a sufficiently large code sample\n\
2354and an exact recipe so that I can reproduce your problem. Failure\n\
2355to do so may mean a greater delay in fixing your bug.\n\n")
2356 (exchange-point-and-mark)
2357 (py-keep-region-active))))
2358
2359
Barry Warsaw7b0f5681995-03-08 21:33:04 +00002360;; arrange to kill temp files when Emacs exists
2361(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
2362 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
2363 ;; have to trust that other people are as respectful of our hook
2364 ;; fiddling as we are of theirs
2365 (if (boundp 'py-inherited-kill-emacs-hook)
2366 ;; we were loaded before -- trust others not to have screwed us
2367 ;; in the meantime (no choice, really)
2368 nil
2369 ;; else arrange for our hook to run theirs
2370 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
2371 (setq kill-emacs-hook 'py-kill-emacs-hook)))
2372
2373
2374
2375(provide 'python-mode)
2376;;; python-mode.el ends here