blob: 35606694f3cb6e4981649b6c4ccc3194087cdee2 [file] [log] [blame]
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001;;; python-mode.el --- Major mode for editing Python programs
2
3;; Copyright (C) 1992,1993,1994 Tim Peters
4
5;; Author: 1995 Barry A. Warsaw <bwarsaw@cnri.reston.va.us>
6;; 1992-1994 Tim Peters <tim@ksr.com>
7;; Maintainer: bwarsaw@cnri.reston.va.us
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +00008;; Created: Feb 1992
Guido van Rossum1d5645d1995-03-14 21:31:47 +00009;; Version: 2.11
10;; Last Modified: 1995/03/14 18:32:54
Guido van Rossumdeaa1051995-03-10 16:17:03 +000011;; Keywords: python editing language major-mode
12
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +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.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000018
19;;; Commentary:
Guido van Rossuma7925f11994-01-26 10:20:16 +000020;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +000021;; This is a major mode for editing Python programs. It was developed
22;; by Tim Peters <tim@ksr.com> after an original idea by Michael
23;; A. Guravage. Tim doesn't appear to be on the 'net any longer so I
Guido van Rossuma8a8d4a1995-03-10 16:19:31 +000024;; have undertaken maintenance of the mode.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000025
26;; At some point this mode will undergo a rewrite to bring it more in
27;; line with GNU Emacs Lisp coding standards. But all in all, the
28;; mode works exceedingly well.
29
30;; The following statements, placed in your .emacs file or
31;; site-init.el, will cause this file to be autoloaded, and
32;; python-mode invoked, when visiting .py files (assuming this file is
33;; in your load-path):
Guido van Rossuma7925f11994-01-26 10:20:16 +000034;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +000035;; (autoload 'python-mode "python-mode" "Python editing mode." t)
Guido van Rossuma7925f11994-01-26 10:20:16 +000036;; (setq auto-mode-alist
37;; (cons '("\\.py$" . python-mode) auto-mode-alist))
38
Guido van Rossum1d5645d1995-03-14 21:31:47 +000039;; Here's a brief list of recent additions/improvements:
40;;
41;; - Wrapping and indentation within triple quote strings should work
42;; properly now.
43;; - `Standard' bug reporting mechanism (use C-c C-b)
44;; - py-mark-block was moved to C-c C-m
45;; - C-c C-v shows you the python-mode version
46;; - a basic python-font-lock-keywords has been added for Emacs 19
47;; font-lock colorizations.
48;; - proper interaction with pending-del and del-sel modes.
49;; - New py-electric-colon (:) command for improved outdenting. Also
50;; py-indent-line (TAB) should handle outdented lines better.
51
Guido van Rossumdeaa1051995-03-10 16:17:03 +000052;; Here's a brief to do list:
53;;
Guido van Rossum1d5645d1995-03-14 21:31:47 +000054;; - Better integration with gud-mode for debugging.
55;; - Rewrite according to GNU Emacs Lisp standards.
56;; - py-delete-char should obey numeric arguments.
57;; - even better support for outdenting. Guido suggests outdents of
58;; at least one level after a return, raise, break, or continue
59;; statement.
Guido van Rossuma7925f11994-01-26 10:20:16 +000060
Guido van Rossumdeaa1051995-03-10 16:17:03 +000061;; If you can think of more things you'd like to see, drop me a line.
62;; If you want to report bugs, use py-submit-bug-report (C-c C-b).
63;;
64;; Note that I only test things on XEmacs (currently 19.11). If you
65;; port stuff to FSF Emacs 19, or Emacs 18, please send me your
66;; patches.
Guido van Rossuma7925f11994-01-26 10:20:16 +000067
Guido van Rossumdeaa1051995-03-10 16:17:03 +000068;; LCD Archive Entry:
69;; python-mode|Barry A. Warsaw|bwarsaw@cnri.reston.va.us
70;; |Major mode for editing Python programs
Guido van Rossum1d5645d1995-03-14 21:31:47 +000071;; |1995/03/14 18:32:54|2.11|
Guido van Rossuma7925f11994-01-26 10:20:16 +000072
Guido van Rossumdeaa1051995-03-10 16:17:03 +000073;;; Code:
74
75
76;; user definable variables
77;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Guido van Rossuma7925f11994-01-26 10:20:16 +000078
79(defvar py-python-command "python"
80 "*Shell command used to start Python interpreter.")
81
82(defvar py-indent-offset 8 ; argue with Guido <grin>
83 "*Indentation increment.
Guido van Rossumdeaa1051995-03-10 16:17:03 +000084Note that `\\[py-guess-indent-offset]' can usually guess a good value
85when you're editing someone else's Python code.")
Guido van Rossuma7925f11994-01-26 10:20:16 +000086
Guido van Rossuma7925f11994-01-26 10:20:16 +000087(defvar py-block-comment-prefix "##"
Guido van Rossumdeaa1051995-03-10 16:17:03 +000088 "*String used by `py-comment-region' to comment out a block of code.
Guido van Rossuma7925f11994-01-26 10:20:16 +000089This should follow the convention for non-indenting comment lines so
90that the indentation commands won't get confused (i.e., the string
91should be of the form `#x...' where `x' is not a blank or a tab, and
92`...' is arbitrary).")
93
94(defvar py-scroll-process-buffer t
95 "*Scroll Python process buffer as output arrives.
96If nil, the Python process buffer acts, with respect to scrolling, like
97Shell-mode buffers normally act. This is surprisingly complicated and
98so won't be explained here; in fact, you can't get the whole story
99without studying the Emacs C code.
100
101If non-nil, the behavior is different in two respects (which are
102slightly inaccurate in the interest of brevity):
103
104 - If the buffer is in a window, and you left point at its end, the
105 window will scroll as new output arrives, and point will move to the
106 buffer's end, even if the window is not the selected window (that
107 being the one the cursor is in). The usual behavior for shell-mode
108 windows is not to scroll, and to leave point where it was, if the
109 buffer is in a window other than the selected window.
110
111 - If the buffer is not visible in any window, and you left point at
112 its end, the buffer will be popped into a window as soon as more
113 output arrives. This is handy if you have a long-running
114 computation and don't want to tie up screen area waiting for the
115 output. The usual behavior for a shell-mode buffer is to stay
116 invisible until you explicitly visit it.
117
118Note the `and if you left point at its end' clauses in both of the
119above: you can `turn off' the special behaviors while output is in
120progress, by visiting the Python buffer and moving point to anywhere
121besides the end. Then the buffer won't scroll, point will remain where
122you leave it, and if you hide the buffer it will stay hidden until you
123visit it again. You can enable and disable the special behaviors as
124often as you like, while output is in progress, by (respectively) moving
125point to, or away from, the end of the buffer.
126
127Warning: If you expect a large amount of output, you'll probably be
128happier setting this option to nil.
129
130Obscure: `End of buffer' above should really say `at or beyond the
131process mark', but if you know what that means you didn't need to be
132told <grin>.")
133
134(defvar py-temp-directory
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000135 (let ((ok '(lambda (x)
136 (and x
137 (setq x (expand-file-name x)) ; always true
138 (file-directory-p x)
139 (file-writable-p x)
140 x))))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000141 (or (funcall ok (getenv "TMPDIR"))
142 (funcall ok "/usr/tmp")
143 (funcall ok "/tmp")
144 (funcall ok ".")
145 (error
146 "Couldn't find a usable temp directory -- set py-temp-directory")))
147 "*Directory used for temp files created by a *Python* process.
148By default, the first directory from this list that exists and that you
149can write into: the value (if any) of the environment variable TMPDIR,
150/usr/tmp, /tmp, or the current directory.")
151
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000152(defvar py-beep-if-tab-change t
153 "*Ring the bell if tab-width is changed.
154If a comment of the form
155
156 \t# vi:set tabsize=<number>:
157
158is found before the first code line when the file is entered, and the
159current value of (the general Emacs variable) `tab-width' does not
160equal <number>, `tab-width' is set to <number>, a message saying so is
161displayed in the echo area, and if `py-beep-if-tab-change' is non-nil
162the Emacs bell is also rung as a warning.")
163
164(defvar python-font-lock-keywords
165 (list
166 (cons
167 (concat
168 "\\<\\("
169 (mapconcat
170 'identity
171 '("access" "and" "break" "continue"
172 "del" "elif" "else" "except"
173 "exec" "finally" "for" "from"
174 "global" "if" "import" "in"
175 "is" "lambda" "not" "or"
176 "pass" "print" "raise" "return"
177 "try" "while" "def" "class"
178 )
179 "\\|")
180 "\\)\\>")
181 1)
182 ;; functions
183 '("\\bdef\\s +\\(\\sw+\\)(" 1 font-lock-function-name-face)
184 ;; classes
185 '("\\bclass\\s +\\(\\sw+\\)[(:]" 1 font-lock-function-name-face)
186 )
187 "*Additional keywords to highlight `python-mode' buffers.")
188
189
190;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
191;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
192
193;; Differentiate between Emacs 18, Lucid Emacs, and Emacs 19. This
194;; seems to be the standard way of checking this.
195;; BAW - This is *not* the right solution. When at all possible,
196;; instead of testing for the version of Emacs, use feature tests.
197
198(setq py-this-is-lucid-emacs-p (string-match "Lucid\\|XEmacs" emacs-version))
199(setq py-this-is-emacs-19-p
200 (and
201 (not py-this-is-lucid-emacs-p)
202 (string-match "^19\\." emacs-version)))
203
Guido van Rossuma7925f11994-01-26 10:20:16 +0000204;; have to bind py-file-queue before installing the kill-emacs hook
205(defvar py-file-queue nil
206 "Queue of Python temp files awaiting execution.
207Currently-active file is at the head of the list.")
208
209;; define a mode-specific abbrev table for those who use such things
210(defvar python-mode-abbrev-table nil
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000211 "Abbrev table in use in `python-mode' buffers.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000212(define-abbrev-table 'python-mode-abbrev-table nil)
213
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000214(defvar python-mode-hook nil
215 "*Hook called by `python-mode'.")
216
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000217;; in previous version of python-mode.el, the hook was incorrectly
218;; called py-mode-hook, and was not defvar'd. deprecate its use.
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000219(and (fboundp 'make-obsolete-variable)
220 (make-obsolete-variable 'py-mode-hook 'python-mode-hook))
221
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000222(defvar py-mode-map ()
223 "Keymap used in `python-mode' buffers.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000224
Guido van Rossuma7925f11994-01-26 10:20:16 +0000225(if py-mode-map
226 ()
227 (setq py-mode-map (make-sparse-keymap))
228
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000229 ;; shadow global bindings for newline-and-indent w/ the py- version.
230 ;; BAW - this is extremely bad form, but I'm not going to change it
231 ;; for now.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000232 (mapcar (function (lambda (key)
233 (define-key
234 py-mode-map key 'py-newline-and-indent)))
235 (where-is-internal 'newline-and-indent))
236
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000237 ;; BAW - you could do it this way, but its not considered proper
238 ;; major-mode form.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000239 (mapcar (function
240 (lambda (x)
241 (define-key py-mode-map (car x) (cdr x))))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000242 '((":" . py-electric-colon)
243 ("\C-c\C-c" . py-execute-buffer)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000244 ("\C-c|" . py-execute-region)
245 ("\C-c!" . py-shell)
246 ("\177" . py-delete-char)
247 ("\n" . py-newline-and-indent)
248 ("\C-c:" . py-guess-indent-offset)
249 ("\C-c\t" . py-indent-region)
250 ("\C-c<" . py-shift-region-left)
251 ("\C-c>" . py-shift-region-right)
252 ("\C-c\C-n" . py-next-statement)
253 ("\C-c\C-p" . py-previous-statement)
254 ("\C-c\C-u" . py-goto-block-up)
255 ("\C-c\C-m" . py-mark-block)
256 ("\C-c#" . py-comment-region)
257 ("\C-c?" . py-describe-mode)
258 ("\C-c\C-hm" . py-describe-mode)
259 ("\e\C-a" . beginning-of-python-def-or-class)
260 ("\e\C-e" . end-of-python-def-or-class)
261 ( "\e\C-h" . mark-python-def-or-class)))
262 ;; should do all keybindings this way
263 (define-key py-mode-map "\C-c\C-b" 'py-submit-bug-report)
264 (define-key py-mode-map "\C-c\C-v" 'py-version)
265 )
Guido van Rossuma7925f11994-01-26 10:20:16 +0000266
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000267(defvar py-mode-syntax-table nil
268 "Syntax table used in `python-mode' buffers.")
269
Guido van Rossuma7925f11994-01-26 10:20:16 +0000270(if py-mode-syntax-table
271 ()
272 (setq py-mode-syntax-table (make-syntax-table))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000273 ;; BAW - again, blech.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000274 (mapcar (function
275 (lambda (x) (modify-syntax-entry
276 (car x) (cdr x) py-mode-syntax-table)))
277 '(( ?\( . "()" ) ( ?\) . ")(" )
278 ( ?\[ . "(]" ) ( ?\] . ")[" )
279 ( ?\{ . "(}" ) ( ?\} . "){" )
280 ;; fix operator symbols misassigned in the std table
281 ( ?\$ . "." ) ( ?\% . "." ) ( ?\& . "." )
282 ( ?\* . "." ) ( ?\+ . "." ) ( ?\- . "." )
283 ( ?\/ . "." ) ( ?\< . "." ) ( ?\= . "." )
284 ( ?\> . "." ) ( ?\| . "." )
285 ( ?\_ . "w" ) ; underscore is legit in names
286 ( ?\' . "\"") ; single quote is string quote
287 ( ?\" . "\"" ) ; double quote is string quote too
288 ( ?\` . "$") ; backquote is open and close paren
289 ( ?\# . "<") ; hash starts comment
290 ( ?\n . ">")))) ; newline ends comment
291
Guido van Rossume531e4b1994-04-16 08:29:27 +0000292(defconst py-stringlit-re
293 (concat
294 "'\\([^'\n\\]\\|\\\\.\\)*'" ; single-quoted
295 "\\|" ; or
296 "\"\\([^\"\n\\]\\|\\\\.\\)*\"") ; double-quoted
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000297 "Regexp matching a Python string literal.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000298
299;; this is tricky because a trailing backslash does not mean
300;; continuation if it's in a comment
301(defconst py-continued-re
302 (concat
Guido van Rossume531e4b1994-04-16 08:29:27 +0000303 "\\(" "[^#'\"\n\\]" "\\|" py-stringlit-re "\\)*"
Guido van Rossuma7925f11994-01-26 10:20:16 +0000304 "\\\\$")
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000305 "Regexp matching Python lines that are continued via backslash.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000306
307(defconst py-blank-or-comment-re "[ \t]*\\($\\|#\\)"
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000308 "Regexp matching blank or comment lines.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000309
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000310(defconst py-outdent-re
311 (concat "\\(" (mapconcat 'identity
312 '("else:"
313 "except\\s +.*:"
314 "finally:"
315 "elif\\s +.*:")
316 "\\|")
317 "\\)")
318 "Regexp matching clauses to be outdented one level.")
Guido van Rossuma7925f11994-01-26 10:20:16 +0000319
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000320
321;;;###autoload
Guido van Rossuma7925f11994-01-26 10:20:16 +0000322(defun python-mode ()
323 "Major mode for editing Python files.
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000324To submit a problem report, enter `\\[py-submit-bug-report]' from a
325`python-mode' buffer. Do `\\[py-describe-mode]' for detailed
326documentation. To see what version of `python-mode' you are running,
327enter `\\[py-version]'.
328
329This mode knows about Python indentation, tokens, comments and
330continuation lines. Paragraphs are separated by blank lines only.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000331
332COMMANDS
333\\{py-mode-map}
334VARIABLES
335
336py-indent-offset\tindentation increment
Guido van Rossuma7925f11994-01-26 10:20:16 +0000337py-block-comment-prefix\tcomment string used by py-comment-region
338py-python-command\tshell command to invoke Python interpreter
339py-scroll-process-buffer\talways scroll Python process buffer
340py-temp-directory\tdirectory used for temp files (if needed)
341py-beep-if-tab-change\tring the bell if tab-width is changed"
342 (interactive)
343 (kill-all-local-variables)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000344 (set-syntax-table py-mode-syntax-table)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000345 (setq major-mode 'python-mode
346 mode-name "Python"
347 local-abbrev-table python-mode-abbrev-table)
348 (use-local-map py-mode-map)
349 ;; BAW -- style...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000350 (mapcar (function (lambda (x)
351 (make-local-variable (car x))
352 (set (car x) (cdr x))))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000353 '((paragraph-separate . "^[ \t]*$")
354 (paragraph-start . "^[ \t]*$")
355 (require-final-newline . t)
356 (comment-start . "# ")
357 (comment-start-skip . "# *")
358 (comment-column . 40)
359 (indent-region-function . py-indent-region)
360 (indent-line-function . py-indent-line)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000361 ;; hack to allow overriding the tabsize in the file (see tokenizer.c)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000362 ;;
363 ;; not sure where the magic comment has to be; to save time
364 ;; searching for a rarity, we give up if it's not found prior to the
365 ;; first executable statement.
366 ;;
367 ;; BAW - on first glance, this seems like complete hackery. Why was
368 ;; this necessary, and is it still necessary?
369 (let ((case-fold-search nil)
370 (start (point))
371 new-tab-width)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000372 (if (re-search-forward
373 "^[ \t]*#[ \t]*vi:set[ \t]+tabsize=\\([0-9]+\\):"
374 (prog2 (py-next-statement 1) (point) (goto-char 1))
375 t)
376 (progn
377 (setq new-tab-width
378 (string-to-int
379 (buffer-substring (match-beginning 1) (match-end 1))))
380 (if (= tab-width new-tab-width)
381 nil
382 (setq tab-width new-tab-width)
383 (message "Caution: tab-width changed to %d" new-tab-width)
384 (if py-beep-if-tab-change (beep)))))
385 (goto-char start))
386
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000387 ;; run the mode hook. py-mode-hook use is deprecated
Guido van Rossuma67bb7e1994-11-10 23:01:59 +0000388 (if python-mode-hook
389 (run-hooks 'python-mode-hook)
390 (run-hooks 'py-mode-hook)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000391
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000392
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000393;; electric characters
394(defun py-electric-colon (arg)
395 "Insert a colon.
396In certain cases the line is outdented appropriately. If a numeric
397argument is provided, that many colons are inserted non-electrically."
398 (interactive "P")
399 (self-insert-command (prefix-numeric-value arg))
400 (let (this-indent)
401 (if (and (not arg)
402 (save-excursion
403 (back-to-indentation)
404 (looking-at py-outdent-re))
405 (= (setq this-indent (py-compute-indentation))
406 (save-excursion
407 (forward-line -1)
408 (py-compute-indentation)))
409 )
410 (save-excursion
411 (beginning-of-line)
412 (delete-horizontal-space)
413 (indent-to (- this-indent py-indent-offset)))
414 )))
415
416
Guido van Rossuma7925f11994-01-26 10:20:16 +0000417;;; Functions that execute Python commands in a subprocess
Guido van Rossuma7925f11994-01-26 10:20:16 +0000418(defun py-shell ()
419 "Start an interactive Python interpreter in another window.
420This is like Shell mode, except that Python is running in the window
421instead of a shell. See the `Interactive Shell' and `Shell Mode'
422sections of the Emacs manual for details, especially for the key
423bindings active in the `*Python*' buffer.
424
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000425See the docs for variable `py-scroll-buffer' for info on scrolling
Guido van Rossuma7925f11994-01-26 10:20:16 +0000426behavior in the process window.
427
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000428Warning: Don't use an interactive Python if you change sys.ps1 or
429sys.ps2 from their default values, or if you're running code that
430prints `>>> ' or `... ' at the start of a line. `python-mode' can't
431distinguish your output from Python's output, and assumes that `>>> '
432at the start of a line is a prompt from Python. Similarly, the Emacs
433Shell mode code assumes that both `>>> ' and `... ' at the start of a
434line are Python prompts. Bad things can happen if you fool either
435mode.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000436
437Warning: If you do any editing *in* the process buffer *while* the
438buffer is accepting output from Python, do NOT attempt to `undo' the
439changes. Some of the output (nowhere near the parts you changed!) may
440be lost if you do. This appears to be an Emacs bug, an unfortunate
441interaction between undo and process filters; the same problem exists in
442non-Python process buffers using the default (Emacs-supplied) process
443filter."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000444 ;; BAW - should undo be disabled in the python process buffer, if
445 ;; this bug still exists?
Guido van Rossuma7925f11994-01-26 10:20:16 +0000446 (interactive)
447 (if py-this-is-emacs-19-p
448 (progn
449 (require 'comint)
450 (switch-to-buffer-other-window
451 (make-comint "Python" py-python-command)))
452 (progn
453 (require 'shell)
454 (switch-to-buffer-other-window
455 (make-shell "Python" py-python-command))))
456 (make-local-variable 'shell-prompt-pattern)
457 (setq shell-prompt-pattern "^>>> \\|^\\.\\.\\. ")
458 (set-process-filter (get-buffer-process (current-buffer))
459 'py-process-filter)
460 (set-syntax-table py-mode-syntax-table))
461
462(defun py-execute-region (start end)
463 "Send the region between START and END to a Python interpreter.
464If there is a *Python* process it is used.
465
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000466Hint: If you want to execute part of a Python file several times
467\(e.g., perhaps you're developing a function and want to flesh it out
468a bit at a time), use `\\[narrow-to-region]' to restrict the buffer to
469the region of interest, and send the code to a *Python* process via
470`\\[py-execute-buffer]' instead.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000471
472Following are subtleties to note when using a *Python* process:
473
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000474If a *Python* process is used, the region is copied into a temporary
475file (in directory `py-temp-directory'), and an `execfile' command is
476sent to Python naming that file. If you send regions faster than
477Python can execute them, `python-mode' will save them into distinct
478temp files, and execute the next one in the queue the next time it
479sees a `>>> ' prompt from Python. Each time this happens, the process
480buffer is popped into a window (if it's not already in some window) so
481you can see it, and a comment of the form
Guido van Rossuma7925f11994-01-26 10:20:16 +0000482
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000483 \t## working on region in file <name> ...
Guido van Rossuma7925f11994-01-26 10:20:16 +0000484
485is inserted at the end.
486
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000487Caution: No more than 26 regions can be pending at any given time.
488This limit is (indirectly) inherited from libc's mktemp(3).
489`python-mode' does not try to protect you from exceeding the limit.
490It's extremely unlikely that you'll get anywhere close to the limit in
491practice, unless you're trying to be a jerk <grin>.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000492
493See the `\\[py-shell]' docs for additional warnings."
494 (interactive "r")
495 (or (< start end) (error "Region is empty"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000496 (let ((pyproc (get-process "Python"))
497 fname)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000498 (if (null pyproc)
499 (shell-command-on-region start end py-python-command)
500 ;; else feed it thru a temp file
501 (setq fname (py-make-temp-name))
502 (write-region start end fname nil 'no-msg)
503 (setq py-file-queue (append py-file-queue (list fname)))
504 (if (cdr py-file-queue)
505 (message "File %s queued for execution" fname)
506 ;; else
507 (py-execute-file pyproc fname)))))
508
509(defun py-execute-file (pyproc fname)
510 (py-append-to-process-buffer
511 pyproc
512 (format "## working on region in file %s ...\n" fname))
513 (process-send-string pyproc (format "execfile('%s')\n" fname)))
514
515(defun py-process-filter (pyproc string)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000516 (let ((curbuf (current-buffer))
517 (pbuf (process-buffer pyproc))
518 (pmark (process-mark pyproc))
519 file-finished)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000520
521 ;; make sure we switch to a different buffer at least once. if we
522 ;; *don't* do this, then if the process buffer is in the selected
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000523 ;; window, and point is before the end, and lots of output is
524 ;; coming at a fast pace, then (a) simple cursor-movement commands
525 ;; like C-p, C-n, C-f, C-b, C-a, C-e take an incredibly long time
526 ;; to have a visible effect (the window just doesn't get updated,
527 ;; sometimes for minutes(!)), and (b) it takes about 5x longer to
528 ;; get all the process output (until the next python prompt).
Guido van Rossuma7925f11994-01-26 10:20:16 +0000529 ;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000530 ;; #b makes no sense to me at all. #a almost makes sense: unless
531 ;; we actually change buffers, set_buffer_internal in buffer.c
532 ;; doesn't set windows_or_buffers_changed to 1, & that in turn
533 ;; seems to make the Emacs command loop reluctant to update the
534 ;; display. Perhaps the default process filter in process.c's
535 ;; read_process_output has update_mode_lines++ for a similar
536 ;; reason? beats me ...
537
538 ;; BAW - we want to check to see if this still applies
Guido van Rossuma7925f11994-01-26 10:20:16 +0000539 (if (eq curbuf pbuf) ; mysterious ugly hack
540 (set-buffer (get-buffer-create "*scratch*")))
541
542 (set-buffer pbuf)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000543 (let* ((start (point))
544 (goback (< start pmark))
545 (buffer-read-only nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000546 (goto-char pmark)
547 (insert string)
548 (move-marker pmark (point))
549 (setq file-finished
550 (and py-file-queue
551 (equal ">>> "
552 (buffer-substring
553 (prog2 (beginning-of-line) (point)
554 (goto-char pmark))
555 (point)))))
556 (if goback (goto-char start)
557 ;; else
558 (if py-scroll-process-buffer
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000559 (let* ((pop-up-windows t)
560 (pwin (display-buffer pbuf)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000561 (set-window-point pwin (point))))))
562 (set-buffer curbuf)
563 (if file-finished
564 (progn
565 (py-delete-file-silently (car py-file-queue))
566 (setq py-file-queue (cdr py-file-queue))
567 (if py-file-queue
568 (py-execute-file pyproc (car py-file-queue)))))))
569
570(defun py-execute-buffer ()
571 "Send the contents of the buffer to a Python interpreter.
572If there is a *Python* process buffer it is used. If a clipping
573restriction is in effect, only the accessible portion of the buffer is
574sent. A trailing newline will be supplied if needed.
575
576See the `\\[py-execute-region]' docs for an account of some subtleties."
577 (interactive)
578 (py-execute-region (point-min) (point-max)))
579
580
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000581
582;; Functions for Python style indentation
Guido van Rossuma7925f11994-01-26 10:20:16 +0000583(defun py-delete-char ()
584 "Reduce indentation or delete character.
585If point is at the leftmost column, deletes the preceding newline.
586
587Else if point is at the leftmost non-blank character of a line that is
588neither a continuation line nor a non-indenting comment line, or if
589point is at the end of a blank line, reduces the indentation to match
590that of the line that opened the current block of code. The line that
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000591opened the block is displayed in the echo area to help you keep track
592of where you are.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000593
594Else the preceding character is deleted, converting a tab to spaces if
595needed so that only a single column position is deleted."
596 (interactive "*")
597 (if (or (/= (current-indentation) (current-column))
598 (bolp)
599 (py-continuation-line-p)
600 (looking-at "#[^ \t\n]")) ; non-indenting #
601 (backward-delete-char-untabify 1)
602 ;; else indent the same as the colon line that opened the block
603
604 ;; force non-blank so py-goto-block-up doesn't ignore it
605 (insert-char ?* 1)
606 (backward-char)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000607 (let ((base-indent 0) ; indentation of base line
608 (base-text "") ; and text of base line
609 (base-found-p nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000610 (condition-case nil ; in case no enclosing block
611 (save-excursion
612 (py-goto-block-up 'no-mark)
613 (setq base-indent (current-indentation)
614 base-text (py-suck-up-leading-text)
615 base-found-p t))
616 (error nil))
617 (delete-char 1) ; toss the dummy character
618 (delete-horizontal-space)
619 (indent-to base-indent)
620 (if base-found-p
621 (message "Closes block: %s" base-text)))))
622
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000623;; required for pending-del and delsel modes
624(put 'py-delete-char 'delete-selection 'supersede)
625(put 'py-delete-char 'pending-delete 'supersede)
626
Guido van Rossuma7925f11994-01-26 10:20:16 +0000627(defun py-indent-line ()
628 "Fix the indentation of the current line according to Python rules."
629 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000630 (let* ((ci (current-indentation))
631 (move-to-indentation-p (<= (current-column) ci))
Guido van Rossum1d5645d1995-03-14 21:31:47 +0000632 (need (py-compute-indentation)))
633 ;; watch for outdents
634 (if (save-excursion
635 (back-to-indentation)
636 (looking-at py-outdent-re))
637 (setq need (- need py-indent-offset)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000638 (if (/= ci need)
639 (save-excursion
640 (beginning-of-line)
641 (delete-horizontal-space)
642 (indent-to need)))
643 (if move-to-indentation-p (back-to-indentation))))
644
645(defun py-newline-and-indent ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000646 "Strives to act like the Emacs `newline-and-indent'.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000647This is just `strives to' because correct indentation can't be computed
648from scratch for Python code. In general, deletes the whitespace before
649point, inserts a newline, and takes an educated guess as to how you want
650the new line indented."
651 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000652 (let ((ci (current-indentation)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000653 (if (< ci (current-column)) ; if point beyond indentation
654 (newline-and-indent)
655 ;; else try to act like newline-and-indent "normally" acts
656 (beginning-of-line)
657 (insert-char ?\n 1)
658 (move-to-column ci))))
659
660(defun py-compute-indentation ()
661 (save-excursion
662 (beginning-of-line)
663 (cond
664 ;; are we on a continuation line?
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000665 ((py-continuation-line-p)
666 (let ((startpos (point))
667 (open-bracket-pos (py-nesting-level))
668 endpos searching found)
669 (if open-bracket-pos
670 (progn
671 ;; align with first item in list; else a normal
672 ;; indent beyond the line with the open bracket
673 (goto-char (1+ open-bracket-pos)) ; just beyond bracket
674 ;; is the first list item on the same line?
675 (skip-chars-forward " \t")
676 (if (null (memq (following-char) '(?\n ?# ?\\)))
677 ; yes, so line up with it
678 (current-column)
679 ;; first list item on another line, or doesn't exist yet
680 (forward-line 1)
681 (while (and (< (point) startpos)
682 (looking-at "[ \t]*[#\n\\\\]")) ; skip noise
683 (forward-line 1))
684 (if (< (point) startpos)
685 ;; again mimic the first list item
686 (current-indentation)
687 ;; else they're about to enter the first item
688 (goto-char open-bracket-pos)
689 (+ (current-indentation) py-indent-offset))))
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +0000690
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000691 ;; else on backslash continuation line
692 (forward-line -1)
693 (if (py-continuation-line-p) ; on at least 3rd line in block
694 (current-indentation) ; so just continue the pattern
695 ;; else started on 2nd line in block, so indent more.
696 ;; if base line is an assignment with a start on a RHS,
697 ;; indent to 2 beyond the leftmost "="; else skip first
698 ;; chunk of non-whitespace characters on base line, + 1 more
699 ;; column
700 (end-of-line)
701 (setq endpos (point) searching t)
702 (back-to-indentation)
703 (setq startpos (point))
704 ;; look at all "=" from left to right, stopping at first
705 ;; one not nested in a list or string
706 (while searching
707 (skip-chars-forward "^=" endpos)
708 (if (= (point) endpos)
709 (setq searching nil)
710 (forward-char 1)
711 (setq state (parse-partial-sexp startpos (point)))
712 (if (and (zerop (car state)) ; not in a bracket
713 (null (nth 3 state))) ; & not in a string
714 (progn
715 (setq searching nil) ; done searching in any case
716 (setq found
717 (not (or
718 (eq (following-char) ?=)
719 (memq (char-after (- (point) 2))
720 '(?< ?> ?!)))))))))
721 (if (or (not found) ; not an assignment
722 (looking-at "[ \t]*\\\\")) ; <=><spaces><backslash>
723 (progn
724 (goto-char startpos)
725 (skip-chars-forward "^ \t\n")))
726 (1+ (current-column))))))
Guido van Rossume531e4b1994-04-16 08:29:27 +0000727
Guido van Rossuma7925f11994-01-26 10:20:16 +0000728 ;; not on a continuation line
729
730 ;; if at start of restriction, or on a non-indenting comment line,
731 ;; assume they intended whatever's there
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000732 ((or (bobp) (looking-at "[ \t]*#[^ \t\n]"))
733 (current-indentation))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000734
735 ;; else indentation based on that of the statement that precedes
736 ;; us; use the first line of that statement to establish the base,
737 ;; in case the user forced a non-std indentation for the
738 ;; continuation lines (if any)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000739 (t
740 ;; skip back over blank & non-indenting comment lines
741 ;; note: will skip a blank or non-indenting comment line that
742 ;; happens to be a continuation line too
743 (re-search-backward "^[ \t]*\\([^ \t\n#]\\|#[ \t\n]\\)"
744 nil 'move)
745 ;; if we landed inside a string, go to the beginning of that
746 ;; string. this handles triple quoted, multi-line spanning
747 ;; strings.
748 (let ((state (parse-partial-sexp
749 (save-excursion (beginning-of-python-def-or-class)
750 (point))
751 (point))))
752 (if (nth 3 state)
753 (goto-char (nth 2 state))))
754 (py-goto-initial-line)
755 (if (py-statement-opens-block-p)
756 (+ (current-indentation) py-indent-offset)
757 (current-indentation))))))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000758
759(defun py-guess-indent-offset (&optional global)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000760 "Guess a good value for, and change, `py-indent-offset'.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000761By default (without a prefix arg), makes a buffer-local copy of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000762`py-indent-offset' with the new value. This will not affect any other
Guido van Rossuma7925f11994-01-26 10:20:16 +0000763Python buffers. With a prefix arg, changes the global value of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000764`py-indent-offset'. This affects all Python buffers (that don't have
Guido van Rossuma7925f11994-01-26 10:20:16 +0000765their own buffer-local copy), both those currently existing and those
766created later in the Emacs session.
767
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000768Some people use a different value for `py-indent-offset' than you use.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000769There's no excuse for such foolishness, but sometimes you have to deal
770with their ugly code anyway. This function examines the file and sets
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000771`py-indent-offset' to what it thinks it was when they created the
772mess.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000773
774Specifically, it searches forward from the statement containing point,
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000775looking for a line that opens a block of code. `py-indent-offset' is
776set to the difference in indentation between that line and the Python
Guido van Rossuma7925f11994-01-26 10:20:16 +0000777statement following it. If the search doesn't succeed going forward,
778it's tried again going backward."
779 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000780 (let (new-value
781 (start (point))
782 restart
783 (found nil)
784 colon-indent)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000785 (py-goto-initial-line)
786 (while (not (or found (eobp)))
787 (if (re-search-forward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
788 (progn
789 (setq restart (point))
790 (py-goto-initial-line)
791 (if (py-statement-opens-block-p)
792 (setq found t)
793 (goto-char restart)))))
794 (if found
795 ()
796 (goto-char start)
797 (py-goto-initial-line)
798 (while (not (or found (bobp)))
799 (setq found
800 (and
801 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
802 (or (py-goto-initial-line) t) ; always true -- side effect
803 (py-statement-opens-block-p)))))
804 (setq colon-indent (current-indentation)
805 found (and found (zerop (py-next-statement 1)))
806 new-value (- (current-indentation) colon-indent))
807 (goto-char start)
808 (if found
809 (progn
810 (funcall (if global 'kill-local-variable 'make-local-variable)
811 'py-indent-offset)
812 (setq py-indent-offset new-value)
813 (message "%s value of py-indent-offset set to %d"
814 (if global "Global" "Local")
815 py-indent-offset))
816 (error "Sorry, couldn't guess a value for py-indent-offset"))))
817
818(defun py-shift-region (start end count)
819 (save-excursion
820 (goto-char end) (beginning-of-line) (setq end (point))
821 (goto-char start) (beginning-of-line) (setq start (point))
822 (indent-rigidly start end count)))
823
824(defun py-shift-region-left (start end &optional count)
825 "Shift region of Python code to the left.
826The lines from the line containing the start of the current region up
827to (but not including) the line containing the end of the region are
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000828shifted to the left, by `py-indent-offset' columns.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000829
830If a prefix argument is given, the region is instead shifted by that
831many columns."
832 (interactive "*r\nP") ; region; raw prefix arg
833 (py-shift-region start end
834 (- (prefix-numeric-value
835 (or count py-indent-offset)))))
836
837(defun py-shift-region-right (start end &optional count)
838 "Shift region of Python code to the right.
839The lines from the line containing the start of the current region up
840to (but not including) the line containing the end of the region are
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000841shifted to the right, by `py-indent-offset' columns.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000842
843If a prefix argument is given, the region is instead shifted by that
844many columns."
845 (interactive "*r\nP") ; region; raw prefix arg
846 (py-shift-region start end (prefix-numeric-value
847 (or count py-indent-offset))))
848
849(defun py-indent-region (start end &optional indent-offset)
850 "Reindent a region of Python code.
851The lines from the line containing the start of the current region up
852to (but not including) the line containing the end of the region are
853reindented. If the first line of the region has a non-whitespace
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000854character in the first column, the first line is left alone and the
855rest of the region is reindented with respect to it. Else the entire
856region is reindented with respect to the (closest code or
857indenting-comment) statement immediately preceding the region.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000858
859This is useful when code blocks are moved or yanked, when enclosing
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000860control structures are introduced or removed, or to reformat code
861using a new value for the indentation offset.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000862
863If a numeric prefix argument is given, it will be used as the value of
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000864the indentation offset. Else the value of `py-indent-offset' will be
Guido van Rossuma7925f11994-01-26 10:20:16 +0000865used.
866
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000867Warning: The region must be consistently indented before this function
Guido van Rossuma7925f11994-01-26 10:20:16 +0000868is called! This function does not compute proper indentation from
869scratch (that's impossible in Python), it merely adjusts the existing
870indentation to be correct in context.
871
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000872Warning: This function really has no idea what to do with
873non-indenting comment lines, and shifts them as if they were indenting
874comment lines. Fixing this appears to require telepathy.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000875
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000876Special cases: whitespace is deleted from blank lines; continuation
877lines are shifted by the same amount their initial line was shifted,
878in order to preserve their relative indentation with respect to their
Guido van Rossuma7925f11994-01-26 10:20:16 +0000879initial line; and comment lines beginning in column 1 are ignored."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000880 (interactive "*r\nP") ; region; raw prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +0000881 (save-excursion
882 (goto-char end) (beginning-of-line) (setq end (point-marker))
883 (goto-char start) (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000884 (let ((py-indent-offset (prefix-numeric-value
885 (or indent-offset py-indent-offset)))
886 (indents '(-1)) ; stack of active indent levels
887 (target-column 0) ; column to which to indent
888 (base-shifted-by 0) ; amount last base line was shifted
889 (indent-base (if (looking-at "[ \t\n]")
890 (py-compute-indentation)
891 0))
892 ci)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000893 (while (< (point) end)
894 (setq ci (current-indentation))
895 ;; figure out appropriate target column
896 (cond
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000897 ((or (eq (following-char) ?#) ; comment in column 1
898 (looking-at "[ \t]*$")) ; entirely blank
899 (setq target-column 0))
900 ((py-continuation-line-p) ; shift relative to base line
901 (setq target-column (+ ci base-shifted-by)))
Guido van Rossuma7925f11994-01-26 10:20:16 +0000902 (t ; new base line
903 (if (> ci (car indents)) ; going deeper; push it
904 (setq indents (cons ci indents))
905 ;; else we should have seen this indent before
906 (setq indents (memq ci indents)) ; pop deeper indents
907 (if (null indents)
908 (error "Bad indentation in region, at line %d"
909 (save-restriction
910 (widen)
911 (1+ (count-lines 1 (point)))))))
912 (setq target-column (+ indent-base
913 (* py-indent-offset
914 (- (length indents) 2))))
915 (setq base-shifted-by (- target-column ci))))
916 ;; shift as needed
917 (if (/= ci target-column)
918 (progn
919 (delete-horizontal-space)
920 (indent-to target-column)))
921 (forward-line 1))))
922 (set-marker end nil))
923
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000924
925;; Functions for moving point
Guido van Rossuma7925f11994-01-26 10:20:16 +0000926(defun py-previous-statement (count)
927 "Go to the start of previous Python statement.
928If the statement at point is the i'th Python statement, goes to the
929start of statement i-COUNT. If there is no such statement, goes to the
930first statement. Returns count of statements left to move.
931`Statements' do not include blank, comment, or continuation lines."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000932 (interactive "p") ; numeric prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +0000933 (if (< count 0) (py-next-statement (- count))
934 (py-goto-initial-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000935 (let (start)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000936 (while (and
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000937 (setq start (point)) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +0000938 (> count 0)
939 (zerop (forward-line -1))
940 (py-goto-statement-at-or-above))
941 (setq count (1- count)))
942 (if (> count 0) (goto-char start)))
943 count))
944
945(defun py-next-statement (count)
946 "Go to the start of next Python statement.
947If the statement at point is the i'th Python statement, goes to the
948start of statement i+COUNT. If there is no such statement, goes to the
949last statement. Returns count of statements left to move. `Statements'
950do not include blank, comment, or continuation lines."
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000951 (interactive "p") ; numeric prefix arg
Guido van Rossuma7925f11994-01-26 10:20:16 +0000952 (if (< count 0) (py-previous-statement (- count))
953 (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000954 (let (start)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000955 (while (and
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000956 (setq start (point)) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +0000957 (> count 0)
958 (py-goto-statement-below))
959 (setq count (1- count)))
960 (if (> count 0) (goto-char start)))
961 count))
962
963(defun py-goto-block-up (&optional nomark)
964 "Move up to start of current block.
965Go to the statement that starts the smallest enclosing block; roughly
966speaking, this will be the closest preceding statement that ends with a
967colon and is indented less than the statement you started on. If
968successful, also sets the mark to the starting point.
969
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000970`\\[py-mark-block]' can be used afterward to mark the whole code
971block, if desired.
Guido van Rossuma7925f11994-01-26 10:20:16 +0000972
973If called from a program, the mark will not be set if optional argument
974NOMARK is not nil."
975 (interactive)
Guido van Rossumdeaa1051995-03-10 16:17:03 +0000976 (let ((start (point))
977 (found nil)
978 initial-indent)
Guido van Rossuma7925f11994-01-26 10:20:16 +0000979 (py-goto-initial-line)
980 ;; if on blank or non-indenting comment line, use the preceding stmt
981 (if (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
982 (progn
983 (py-goto-statement-at-or-above)
984 (setq found (py-statement-opens-block-p))))
985 ;; search back for colon line indented less
986 (setq initial-indent (current-indentation))
987 (if (zerop initial-indent)
988 ;; force fast exit
989 (goto-char (point-min)))
990 (while (not (or found (bobp)))
991 (setq found
992 (and
993 (re-search-backward ":[ \t]*\\($\\|[#\\]\\)" nil 'move)
994 (or (py-goto-initial-line) t) ; always true -- side effect
995 (< (current-indentation) initial-indent)
996 (py-statement-opens-block-p))))
997 (if found
998 (progn
999 (or nomark (push-mark start))
1000 (back-to-indentation))
1001 (goto-char start)
1002 (error "Enclosing block not found"))))
1003
1004(defun beginning-of-python-def-or-class (&optional class)
1005 "Move point to start of def (or class, with prefix arg).
1006
1007Searches back for the closest preceding `def'. If you supply a prefix
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001008arg, looks for a `class' instead. The docs assume the `def' case;
1009just substitute `class' for `def' for the other case.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001010
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001011If point is in a def statement already, and after the `d', simply
1012moves point to the start of the statement.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001013
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001014Else (point is not in a def statement, or at or before the `d' of a
1015def statement), searches for the closest preceding def statement, and
1016leaves point at its start. If no such statement can be found, leaves
1017point at the start of the buffer.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001018
1019Returns t iff a def statement is found by these rules.
1020
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001021Note that doing this command repeatedly will take you closer to the
1022start of the buffer each time.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001023
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001024If you want to mark the current def/class, see
1025`\\[mark-python-def-or-class]'."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001026 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001027 (let ((at-or-before-p (<= (current-column) (current-indentation)))
1028 (start-of-line (progn (beginning-of-line) (point)))
1029 (start-of-stmt (progn (py-goto-initial-line) (point))))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001030 (if (or (/= start-of-stmt start-of-line)
1031 (not at-or-before-p))
1032 (end-of-line)) ; OK to match on this line
1033 (re-search-backward (if class "^[ \t]*class\\>" "^[ \t]*def\\>")
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001034 nil 'move)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001035
1036(defun end-of-python-def-or-class (&optional class)
1037 "Move point beyond end of def (or class, with prefix arg) body.
1038
1039By default, looks for an appropriate `def'. If you supply a prefix arg,
1040looks for a `class' instead. The docs assume the `def' case; just
1041substitute `class' for `def' for the other case.
1042
1043If point is in a def statement already, this is the def we use.
1044
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001045Else if the def found by `\\[beginning-of-python-def-or-class]'
1046contains the statement you started on, that's the def we use.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001047
1048Else we search forward for the closest following def, and use that.
1049
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001050If a def can be found by these rules, point is moved to the start of
1051the line immediately following the def block, and the position of the
1052start of the def is returned.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001053
1054Else point is moved to the end of the buffer, and nil is returned.
1055
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001056Note that doing this command repeatedly will take you closer to the
1057end of the buffer each time.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001058
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001059If you want to mark the current def/class, see
1060`\\[mark-python-def-or-class]'."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001061 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001062 (let ((start (progn (py-goto-initial-line) (point)))
1063 (which (if class "class" "def"))
1064 (state 'not-found))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001065 ;; move point to start of appropriate def/class
1066 (if (looking-at (concat "[ \t]*" which "\\>")) ; already on one
1067 (setq state 'at-beginning)
1068 ;; else see if beginning-of-python-def-or-class hits container
1069 (if (and (beginning-of-python-def-or-class class)
1070 (progn (py-goto-beyond-block)
1071 (> (point) start)))
1072 (setq state 'at-end)
1073 ;; else search forward
1074 (goto-char start)
1075 (if (re-search-forward (concat "^[ \t]*" which "\\>") nil 'move)
1076 (progn (setq state 'at-beginning)
1077 (beginning-of-line)))))
1078 (cond
1079 ((eq state 'at-beginning) (py-goto-beyond-block) t)
1080 ((eq state 'at-end) t)
1081 ((eq state 'not-found) nil)
1082 (t (error "internal error in end-of-python-def-or-class")))))
1083
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001084
1085;; Functions for marking regions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001086(defun py-mark-block (&optional extend just-move)
1087 "Mark following block of lines. With prefix arg, mark structure.
1088Easier to use than explain. It sets the region to an `interesting'
1089block of succeeding lines. If point is on a blank line, it goes down to
1090the next non-blank line. That will be the start of the region. The end
1091of the region depends on the kind of line at the start:
1092
1093 - If a comment, the region will include all succeeding comment lines up
1094 to (but not including) the next non-comment line (if any).
1095
1096 - Else if a prefix arg is given, and the line begins one of these
1097 structures:
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001098
1099 if elif else try except finally for while def class
1100
Guido van Rossuma7925f11994-01-26 10:20:16 +00001101 the region will be set to the body of the structure, including
1102 following blocks that `belong' to it, but excluding trailing blank
1103 and comment lines. E.g., if on a `try' statement, the `try' block
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001104 and all (if any) of the following `except' and `finally' blocks
1105 that belong to the `try' structure will be in the region. Ditto
1106 for if/elif/else, for/else and while/else structures, and (a bit
1107 degenerate, since they're always one-block structures) def and
1108 class blocks.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001109
1110 - Else if no prefix argument is given, and the line begins a Python
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001111 block (see list above), and the block is not a `one-liner' (i.e.,
1112 the statement ends with a colon, not with code), the region will
1113 include all succeeding lines up to (but not including) the next
1114 code statement (if any) that's indented no more than the starting
1115 line, except that trailing blank and comment lines are excluded.
1116 E.g., if the starting line begins a multi-statement `def'
1117 structure, the region will be set to the full function definition,
1118 but without any trailing `noise' lines.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001119
1120 - Else the region will include all succeeding lines up to (but not
1121 including) the next blank line, or code or indenting-comment line
1122 indented strictly less than the starting line. Trailing indenting
1123 comment lines are included in this case, but not trailing blank
1124 lines.
1125
1126A msg identifying the location of the mark is displayed in the echo
1127area; or do `\\[exchange-point-and-mark]' to flip down to the end.
1128
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001129If called from a program, optional argument EXTEND plays the role of
1130the prefix arg, and if optional argument JUST-MOVE is not nil, just
1131moves to the end of the block (& does not set mark or display a msg)."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001132 (interactive "P") ; raw prefix arg
1133 (py-goto-initial-line)
1134 ;; skip over blank lines
1135 (while (and
1136 (looking-at "[ \t]*$") ; while blank line
1137 (not (eobp))) ; & somewhere to go
1138 (forward-line 1))
1139 (if (eobp)
1140 (error "Hit end of buffer without finding a non-blank stmt"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001141 (let ((initial-pos (point))
1142 (initial-indent (current-indentation))
1143 last-pos ; position of last stmt in region
1144 (followers
1145 '((if elif else) (elif elif else) (else)
1146 (try except finally) (except except) (finally)
1147 (for else) (while else)
1148 (def) (class) ) )
1149 first-symbol next-symbol)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001150
1151 (cond
1152 ;; if comment line, suck up the following comment lines
1153 ((looking-at "[ \t]*#")
1154 (re-search-forward "^[ \t]*[^ \t#]" nil 'move) ; look for non-comment
1155 (re-search-backward "^[ \t]*#") ; and back to last comment in block
1156 (setq last-pos (point)))
1157
1158 ;; else if line is a block line and EXTEND given, suck up
1159 ;; the whole structure
1160 ((and extend
1161 (setq first-symbol (py-suck-up-first-keyword) )
1162 (assq first-symbol followers))
1163 (while (and
1164 (or (py-goto-beyond-block) t) ; side effect
1165 (forward-line -1) ; side effect
1166 (setq last-pos (point)) ; side effect
1167 (py-goto-statement-below)
1168 (= (current-indentation) initial-indent)
1169 (setq next-symbol (py-suck-up-first-keyword))
1170 (memq next-symbol (cdr (assq first-symbol followers))))
1171 (setq first-symbol next-symbol)))
1172
1173 ;; else if line *opens* a block, search for next stmt indented <=
1174 ((py-statement-opens-block-p)
1175 (while (and
1176 (setq last-pos (point)) ; always true -- side effect
1177 (py-goto-statement-below)
1178 (> (current-indentation) initial-indent))
1179 nil))
1180
1181 ;; else plain code line; stop at next blank line, or stmt or
1182 ;; indenting comment line indented <
1183 (t
1184 (while (and
1185 (setq last-pos (point)) ; always true -- side effect
1186 (or (py-goto-beyond-final-line) t)
1187 (not (looking-at "[ \t]*$")) ; stop at blank line
1188 (or
1189 (>= (current-indentation) initial-indent)
1190 (looking-at "[ \t]*#[^ \t\n]"))) ; ignore non-indenting #
1191 nil)))
1192
1193 ;; skip to end of last stmt
1194 (goto-char last-pos)
1195 (py-goto-beyond-final-line)
1196
1197 ;; set mark & display
1198 (if just-move
1199 () ; just return
1200 (push-mark (point) 'no-msg)
1201 (forward-line -1)
1202 (message "Mark set after: %s" (py-suck-up-leading-text))
1203 (goto-char initial-pos))))
1204
1205(defun mark-python-def-or-class (&optional class)
1206 "Set region to body of def (or class, with prefix arg) enclosing point.
1207Pushes the current mark, then point, on the mark ring (all language
1208modes do this, but although it's handy it's never documented ...).
1209
1210In most Emacs language modes, this function bears at least a
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001211hallucinogenic resemblance to `\\[end-of-python-def-or-class]' and
1212`\\[beginning-of-python-def-or-class]'.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001213
1214And in earlier versions of Python mode, all 3 were tightly connected.
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001215Turned out that was more confusing than useful: the `goto start' and
1216`goto end' commands are usually used to search through a file, and
1217people expect them to act a lot like `search backward' and `search
1218forward' string-search commands. But because Python `def' and `class'
1219can nest to arbitrary levels, finding the smallest def containing
1220point cannot be done via a simple backward search: the def containing
1221point may not be the closest preceding def, or even the closest
1222preceding def that's indented less. The fancy algorithm required is
1223appropriate for the usual uses of this `mark' command, but not for the
1224`goto' variations.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001225
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001226So the def marked by this command may not be the one either of the
1227`goto' commands find: If point is on a blank or non-indenting comment
1228line, moves back to start of the closest preceding code statement or
1229indenting comment line. If this is a `def' statement, that's the def
1230we use. Else searches for the smallest enclosing `def' block and uses
1231that. Else signals an error.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001232
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001233When an enclosing def is found: The mark is left immediately beyond
1234the last line of the def block. Point is left at the start of the
1235def, except that: if the def is preceded by a number of comment lines
1236followed by (at most) one optional blank line, point is left at the
1237start of the comments; else if the def is preceded by a blank line,
1238point is left at its start.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001239
1240The intent is to mark the containing def/class and its associated
1241documentation, to make moving and duplicating functions and classes
1242pleasant."
1243 (interactive "P") ; raw prefix arg
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001244 (let ((start (point))
1245 (which (if class "class" "def")))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001246 (push-mark start)
1247 (if (not (py-go-up-tree-to-keyword which))
1248 (progn (goto-char start)
1249 (error "Enclosing %s not found" which))
1250 ;; else enclosing def/class found
1251 (setq start (point))
1252 (py-goto-beyond-block)
1253 (push-mark (point))
1254 (goto-char start)
1255 (if (zerop (forward-line -1)) ; if there is a preceding line
1256 (progn
1257 (if (looking-at "[ \t]*$") ; it's blank
1258 (setq start (point)) ; so reset start point
1259 (goto-char start)) ; else try again
1260 (if (zerop (forward-line -1))
1261 (if (looking-at "[ \t]*#") ; a comment
1262 ;; look back for non-comment line
1263 ;; tricky: note that the regexp matches a blank
1264 ;; line, cuz \n is in the 2nd character class
1265 (and
1266 (re-search-backward "^[ \t]*[^ \t#]" nil 'move)
1267 (forward-line 1))
1268 ;; no comment, so go back
1269 (goto-char start))))))))
1270
1271(defun py-comment-region (start end &optional uncomment-p)
1272 "Comment out region of code; with prefix arg, uncomment region.
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
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001275commented out, by inserting the string `py-block-comment-prefix' at
1276the start of each line. With a prefix arg, removes
1277`py-block-comment-prefix' from the start of each line instead."
Guido van Rossuma7925f11994-01-26 10:20:16 +00001278 (interactive "*r\nP") ; region; raw prefix arg
1279 (goto-char end) (beginning-of-line) (setq end (point))
1280 (goto-char start) (beginning-of-line) (setq start (point))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001281 (let ((prefix-len (length py-block-comment-prefix)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001282 (save-excursion
1283 (save-restriction
1284 (narrow-to-region start end)
1285 (while (not (eobp))
1286 (if uncomment-p
1287 (and (string= py-block-comment-prefix
1288 (buffer-substring
1289 (point) (+ (point) prefix-len)))
1290 (delete-char prefix-len))
1291 (insert py-block-comment-prefix))
1292 (forward-line 1))))))
1293
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001294
1295;; Documentation functions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001296
1297;; dump the long form of the mode blurb; does the usual doc escapes,
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001298;; plus lines of the form ^[vc]:name$ to suck variable & command docs
1299;; out of the right places, along with the keys they're on & current
1300;; values
Guido van Rossuma7925f11994-01-26 10:20:16 +00001301(defun py-dump-help-string (str)
1302 (with-output-to-temp-buffer "*Help*"
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001303 (let ((locals (buffer-local-variables))
1304 funckind funcname func funcdoc
1305 (start 0) mstart end
1306 keys )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001307 (while (string-match "^%\\([vc]\\):\\(.+\\)\n" str start)
1308 (setq mstart (match-beginning 0) end (match-end 0)
1309 funckind (substring str (match-beginning 1) (match-end 1))
1310 funcname (substring str (match-beginning 2) (match-end 2))
1311 func (intern funcname))
1312 (princ (substitute-command-keys (substring str start mstart)))
1313 (cond
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001314 ((equal funckind "c") ; command
1315 (setq funcdoc (documentation func)
1316 keys (concat
1317 "Key(s): "
1318 (mapconcat 'key-description
1319 (where-is-internal func py-mode-map)
1320 ", "))))
1321 ((equal funckind "v") ; variable
1322 (setq funcdoc (substitute-command-keys
1323 (get func 'variable-documentation))
1324 keys (if (assq func locals)
1325 (concat
1326 "Local/Global values: "
1327 (prin1-to-string (symbol-value func))
1328 " / "
1329 (prin1-to-string (default-value func)))
1330 (concat
1331 "Value: "
1332 (prin1-to-string (symbol-value func))))))
1333 (t ; unexpected
1334 (error "Error in py-dump-help-string, tag `%s'" funckind)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001335 (princ (format "\n-> %s:\t%s\t%s\n\n"
1336 (if (equal funckind "c") "Command" "Variable")
1337 funcname keys))
1338 (princ funcdoc)
1339 (terpri)
1340 (setq start end))
1341 (princ (substitute-command-keys (substring str start))))
1342 (print-help-return-message)))
1343
1344(defun py-describe-mode ()
1345 "Dump long form of Python-mode docs."
1346 (interactive)
1347 (py-dump-help-string "Major mode for editing Python files.
1348Knows about Python indentation, tokens, comments and continuation lines.
1349Paragraphs are separated by blank lines only.
1350
1351Major sections below begin with the string `@'; specific function and
1352variable docs begin with `->'.
1353
1354@EXECUTING PYTHON CODE
1355
1356\\[py-execute-buffer]\tsends the entire buffer to the Python interpreter
1357\\[py-execute-region]\tsends the current region
1358\\[py-shell]\tstarts a Python interpreter window; this will be used by
1359\tsubsequent \\[py-execute-buffer] or \\[py-execute-region] commands
1360%c:py-execute-buffer
1361%c:py-execute-region
1362%c:py-shell
1363
1364@VARIABLES
1365
1366py-indent-offset\tindentation increment
Guido van Rossuma7925f11994-01-26 10:20:16 +00001367py-block-comment-prefix\tcomment string used by py-comment-region
1368
1369py-python-command\tshell command to invoke Python interpreter
1370py-scroll-process-buffer\talways scroll Python process buffer
1371py-temp-directory\tdirectory used for temp files (if needed)
1372
1373py-beep-if-tab-change\tring the bell if tab-width is changed
1374%v:py-indent-offset
Guido van Rossuma7925f11994-01-26 10:20:16 +00001375%v:py-block-comment-prefix
1376%v:py-python-command
1377%v:py-scroll-process-buffer
1378%v:py-temp-directory
1379%v:py-beep-if-tab-change
1380
1381@KINDS OF LINES
1382
1383Each physical line in the file is either a `continuation line' (the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001384preceding line ends with a backslash that's not part of a comment, or
1385the paren/bracket/brace nesting level at the start of the line is
1386non-zero, or both) or an `initial line' (everything else).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001387
1388An initial line is in turn a `blank line' (contains nothing except
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001389possibly blanks or tabs), a `comment line' (leftmost non-blank
1390character is `#'), or a `code line' (everything else).
Guido van Rossuma7925f11994-01-26 10:20:16 +00001391
1392Comment Lines
1393
1394Although all comment lines are treated alike by Python, Python mode
1395recognizes two kinds that act differently with respect to indentation.
1396
1397An `indenting comment line' is a comment line with a blank, tab or
1398nothing after the initial `#'. The indentation commands (see below)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001399treat these exactly as if they were code lines: a line following an
Guido van Rossuma7925f11994-01-26 10:20:16 +00001400indenting comment line will be indented like the comment line. All
1401other comment lines (those with a non-whitespace character immediately
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001402following the initial `#') are `non-indenting comment lines', and
1403their indentation is ignored by the indentation commands.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001404
1405Indenting comment lines are by far the usual case, and should be used
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001406whenever possible. Non-indenting comment lines are useful in cases
1407like these:
Guido van Rossuma7925f11994-01-26 10:20:16 +00001408
1409\ta = b # a very wordy single-line comment that ends up being
1410\t #... continued onto another line
1411
1412\tif a == b:
1413##\t\tprint 'panic!' # old code we've `commented out'
1414\t\treturn a
1415
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001416Since the `#...' and `##' comment lines have a non-whitespace
1417character following the initial `#', Python mode ignores them when
1418computing the proper indentation for the next line.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001419
1420Continuation Lines and Statements
1421
1422The Python-mode commands generally work on statements instead of on
1423individual lines, where a `statement' is a comment or blank line, or a
1424code line and all of its following continuation lines (if any)
1425considered as a single logical unit. The commands in this mode
1426generally (when it makes sense) automatically move to the start of the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001427statement containing point, even if point happens to be in the middle
1428of some continuation line.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001429
Guido van Rossuma7925f11994-01-26 10:20:16 +00001430
1431@INDENTATION
1432
1433Primarily for entering new code:
1434\t\\[indent-for-tab-command]\t indent line appropriately
1435\t\\[py-newline-and-indent]\t insert newline, then indent
1436\t\\[py-delete-char]\t reduce indentation, or delete single character
1437
1438Primarily for reindenting existing code:
1439\t\\[py-guess-indent-offset]\t guess py-indent-offset from file content; change locally
1440\t\\[universal-argument] \\[py-guess-indent-offset]\t ditto, but change globally
1441
1442\t\\[py-indent-region]\t reindent region to match its context
1443\t\\[py-shift-region-left]\t shift region left by py-indent-offset
1444\t\\[py-shift-region-right]\t shift region right by py-indent-offset
1445
1446Unlike most programming languages, Python uses indentation, and only
1447indentation, to specify block structure. Hence the indentation supplied
1448automatically by Python-mode is just an educated guess: only you know
1449the block structure you intend, so only you can supply correct
1450indentation.
1451
1452The \\[indent-for-tab-command] and \\[py-newline-and-indent] keys try to suggest plausible indentation, based on
1453the indentation of preceding statements. E.g., assuming
1454py-indent-offset is 4, after you enter
1455\tif a > 0: \\[py-newline-and-indent]
1456the cursor will be moved to the position of the `_' (_ is not a
1457character in the file, it's just used here to indicate the location of
1458the cursor):
1459\tif a > 0:
1460\t _
1461If you then enter `c = d' \\[py-newline-and-indent], the cursor will move
1462to
1463\tif a > 0:
1464\t c = d
1465\t _
1466Python-mode cannot know whether that's what you intended, or whether
1467\tif a > 0:
1468\t c = d
1469\t_
1470was your intent. In general, Python-mode either reproduces the
1471indentation of the (closest code or indenting-comment) preceding
1472statement, or adds an extra py-indent-offset blanks if the preceding
1473statement has `:' as its last significant (non-whitespace and non-
1474comment) character. If the suggested indentation is too much, use
1475\\[py-delete-char] to reduce it.
1476
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +00001477Continuation lines are given extra indentation. If you don't like the
1478suggested indentation, change it to something you do like, and Python-
1479mode will strive to indent later lines of the statement in the same way.
1480
1481If a line is a continuation line by virtue of being in an unclosed
1482paren/bracket/brace structure (`list', for short), the suggested
Guido van Rossum9274e2d1994-04-26 07:35:17 +00001483indentation depends on whether the current line contains the first item
1484in the list. If it does, it's indented py-indent-offset columns beyond
1485the indentation of the line containing the open bracket. If you don't
1486like that, change it by hand. The remaining items in the list will mimic
1487whatever indentation you give to the first item.
Guido van Rossume531e4b1994-04-16 08:29:27 +00001488
1489If a line is a continuation line because the line preceding it ends with
Guido van Rossum0ec5c5d1994-04-25 08:12:43 +00001490a backslash, the third and following lines of the statement inherit their
1491indentation from the line preceding them. The indentation of the second
1492line in the statement depends on the form of the first (base) line: if
1493the base line is an assignment statement with anything more interesting
1494than the backslash following the leftmost assigning `=', the second line
1495is indented two columns beyond that `='. Else it's indented to two
1496columns beyond the leftmost solid chunk of non-whitespace characters on
1497the base line.
Guido van Rossume531e4b1994-04-16 08:29:27 +00001498
Guido van Rossuma7925f11994-01-26 10:20:16 +00001499Warning: indent-region should not normally be used! It calls \\[indent-for-tab-command]
1500repeatedly, and as explained above, \\[indent-for-tab-command] can't guess the block
1501structure you intend.
1502%c:indent-for-tab-command
1503%c:py-newline-and-indent
1504%c:py-delete-char
1505
1506
1507The next function may be handy when editing code you didn't write:
1508%c:py-guess-indent-offset
1509
1510
1511The remaining `indent' functions apply to a region of Python code. They
1512assume the block structure (equals indentation, in Python) of the region
1513is correct, and alter the indentation in various ways while preserving
1514the block structure:
1515%c:py-indent-region
1516%c:py-shift-region-left
1517%c:py-shift-region-right
1518
1519@MARKING & MANIPULATING REGIONS OF CODE
1520
1521\\[py-mark-block]\t mark block of lines
1522\\[mark-python-def-or-class]\t mark smallest enclosing def
1523\\[universal-argument] \\[mark-python-def-or-class]\t mark smallest enclosing class
1524\\[py-comment-region]\t comment out region of code
1525\\[universal-argument] \\[py-comment-region]\t uncomment region of code
1526%c:py-mark-block
1527%c:mark-python-def-or-class
1528%c:py-comment-region
1529
1530@MOVING POINT
1531
1532\\[py-previous-statement]\t move to statement preceding point
1533\\[py-next-statement]\t move to statement following point
1534\\[py-goto-block-up]\t move up to start of current block
1535\\[beginning-of-python-def-or-class]\t move to start of def
1536\\[universal-argument] \\[beginning-of-python-def-or-class]\t move to start of class
1537\\[end-of-python-def-or-class]\t move to end of def
1538\\[universal-argument] \\[end-of-python-def-or-class]\t move to end of class
1539
1540The first two move to one statement beyond the statement that contains
1541point. A numeric prefix argument tells them to move that many
1542statements instead. Blank lines, comment lines, and continuation lines
1543do not count as `statements' for these commands. So, e.g., you can go
1544to the first code statement in a file by entering
1545\t\\[beginning-of-buffer]\t to move to the top of the file
1546\t\\[py-next-statement]\t to skip over initial comments and blank lines
1547Or do `\\[py-previous-statement]' with a huge prefix argument.
1548%c:py-previous-statement
1549%c:py-next-statement
1550%c:py-goto-block-up
1551%c:beginning-of-python-def-or-class
1552%c:end-of-python-def-or-class
1553
1554@LITTLE-KNOWN EMACS COMMANDS PARTICULARLY USEFUL IN PYTHON MODE
1555
1556`\\[indent-new-comment-line]' is handy for entering a multi-line comment.
1557
1558`\\[set-selective-display]' with a `small' prefix arg is ideally suited for viewing the
1559overall class and def structure of a module.
1560
1561`\\[back-to-indentation]' moves point to a line's first non-blank character.
1562
1563`\\[indent-relative]' is handy for creating odd indentation.
1564
1565@OTHER EMACS HINTS
1566
1567If you don't like the default value of a variable, change its value to
1568whatever you do like by putting a `setq' line in your .emacs file.
1569E.g., to set the indentation increment to 4, put this line in your
1570.emacs:
1571\t(setq py-indent-offset 4)
1572To see the value of a variable, do `\\[describe-variable]' and enter the variable
1573name at the prompt.
1574
1575When entering a key sequence like `C-c C-n', it is not necessary to
1576release the CONTROL key after doing the `C-c' part -- it suffices to
1577press the CONTROL key, press and release `c' (while still holding down
1578CONTROL), press and release `n' (while still holding down CONTROL), &
1579then release CONTROL.
1580
1581Entering Python mode calls with no arguments the value of the variable
Guido van Rossuma67bb7e1994-11-10 23:01:59 +00001582`python-mode-hook', if that value exists and is not nil; for backward
1583compatibility it also tries `py-mode-hook'; see the `Hooks' section of
1584the Elisp manual for details.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001585
1586Obscure: When python-mode is first loaded, it looks for all bindings
1587to newline-and-indent in the global keymap, and shadows them with
1588local bindings to py-newline-and-indent."))
1589
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001590
1591;; Helper functions
Guido van Rossuma7925f11994-01-26 10:20:16 +00001592(defvar py-parse-state-re
1593 (concat
1594 "^[ \t]*\\(if\\|elif\\|else\\|while\\|def\\|class\\)\\>"
1595 "\\|"
1596 "^[^ #\t\n]"))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001597
Guido van Rossuma7925f11994-01-26 10:20:16 +00001598;; returns the parse state at point (see parse-partial-sexp docs)
1599(defun py-parse-state ()
1600 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001601 (let ((here (point)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001602 ;; back up to the first preceding line (if any; else start of
1603 ;; buffer) that begins with a popular Python keyword, or a non-
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001604 ;; whitespace and non-comment character. These are good places
1605 ;; to start parsing to see whether where we started is at a
1606 ;; non-zero nesting level. It may be slow for people who write
1607 ;; huge code blocks or huge lists ... tough beans.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001608 (re-search-backward py-parse-state-re nil 'move)
1609 (beginning-of-line)
1610 (parse-partial-sexp (point) here))))
1611
1612;; if point is at a non-zero nesting level, returns the number of the
1613;; character that opens the smallest enclosing unclosed list; else
1614;; returns nil.
1615(defun py-nesting-level ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001616 (let ((status (py-parse-state)) )
Guido van Rossuma7925f11994-01-26 10:20:16 +00001617 (if (zerop (car status))
1618 nil ; not in a nest
1619 (car (cdr status))))) ; char# of open bracket
1620
1621;; t iff preceding line ends with backslash that's not in a comment
1622(defun py-backslash-continuation-line-p ()
1623 (save-excursion
1624 (beginning-of-line)
1625 (and
1626 ;; use a cheap test first to avoid the regexp if possible
1627 ;; use 'eq' because char-after may return nil
1628 (eq (char-after (- (point) 2)) ?\\ )
1629 ;; make sure; since eq test passed, there is a preceding line
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001630 (forward-line -1) ; always true -- side effect
Guido van Rossuma7925f11994-01-26 10:20:16 +00001631 (looking-at py-continued-re))))
1632
1633;; t iff current line is a continuation line
1634(defun py-continuation-line-p ()
1635 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001636 (beginning-of-line)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001637 (or (py-backslash-continuation-line-p)
1638 (py-nesting-level))))
1639
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001640;; go to initial line of current statement; usually this is the line
1641;; we're on, but if we're on the 2nd or following lines of a
1642;; continuation block, we need to go up to the first line of the
1643;; block.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001644;;
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001645;; Tricky: We want to avoid quadratic-time behavior for long continued
1646;; blocks, whether of the backslash or open-bracket varieties, or a
1647;; mix of the two. The following manages to do that in the usual
1648;; cases.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001649(defun py-goto-initial-line ()
1650 (let ( open-bracket-pos )
1651 (while (py-continuation-line-p)
1652 (beginning-of-line)
1653 (if (py-backslash-continuation-line-p)
1654 (while (py-backslash-continuation-line-p)
1655 (forward-line -1))
1656 ;; else zip out of nested brackets/braces/parens
1657 (while (setq open-bracket-pos (py-nesting-level))
1658 (goto-char open-bracket-pos)))))
1659 (beginning-of-line))
1660
1661;; go to point right beyond final line of current statement; usually
1662;; this is the start of the next line, but if this is a multi-line
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001663;; statement we need to skip over the continuation lines. Tricky:
1664;; Again we need to be clever to avoid quadratic time behavior.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001665(defun py-goto-beyond-final-line ()
1666 (forward-line 1)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001667 (let (state)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001668 (while (and (py-continuation-line-p)
1669 (not (eobp)))
1670 ;; skip over the backslash flavor
1671 (while (and (py-backslash-continuation-line-p)
1672 (not (eobp)))
1673 (forward-line 1))
1674 ;; if in nest, zip to the end of the nest
1675 (setq state (py-parse-state))
1676 (if (and (not (zerop (car state)))
1677 (not (eobp)))
1678 (progn
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001679 ;; BUG ALERT: I could swear, from reading the docs, that
Guido van Rossuma7925f11994-01-26 10:20:16 +00001680 ;; the 3rd argument should be plain 0
1681 (parse-partial-sexp (point) (point-max) (- 0 (car state))
1682 nil state)
1683 (forward-line 1))))))
1684
1685;; t iff statement opens a block == iff it ends with a colon that's
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001686;; not in a comment. point should be at the start of a statement
Guido van Rossuma7925f11994-01-26 10:20:16 +00001687(defun py-statement-opens-block-p ()
1688 (save-excursion
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001689 (let ((start (point))
1690 (finish (progn (py-goto-beyond-final-line) (1- (point))))
1691 (searching t)
1692 (answer nil)
1693 state)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001694 (goto-char start)
1695 (while searching
1696 ;; look for a colon with nothing after it except whitespace, and
1697 ;; maybe a comment
1698 (if (re-search-forward ":\\([ \t]\\|\\\\\n\\)*\\(#.*\\)?$"
1699 finish t)
1700 (if (eq (point) finish) ; note: no `else' clause; just
1701 ; keep searching if we're not at
1702 ; the end yet
1703 ;; sure looks like it opens a block -- but it might
1704 ;; be in a comment
1705 (progn
1706 (setq searching nil) ; search is done either way
1707 (setq state (parse-partial-sexp start
1708 (match-beginning 0)))
1709 (setq answer (not (nth 4 state)))))
1710 ;; search failed: couldn't find another interesting colon
1711 (setq searching nil)))
1712 answer)))
1713
1714;; go to point right beyond final line of block begun by the current
1715;; line. This is the same as where py-goto-beyond-final-line goes
1716;; unless we're on colon line, in which case we go to the end of the
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001717;; block. assumes point is at bolp
Guido van Rossuma7925f11994-01-26 10:20:16 +00001718(defun py-goto-beyond-block ()
1719 (if (py-statement-opens-block-p)
1720 (py-mark-block nil 'just-move)
1721 (py-goto-beyond-final-line)))
1722
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001723;; go to start of first statement (not blank or comment or
1724;; continuation line) at or preceding point. returns t if there is
1725;; one, else nil
Guido van Rossuma7925f11994-01-26 10:20:16 +00001726(defun py-goto-statement-at-or-above ()
1727 (py-goto-initial-line)
1728 (if (looking-at py-blank-or-comment-re)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001729 ;; skip back over blank & comment lines
1730 ;; note: will skip a blank or comment line that happens to be
1731 ;; a continuation line too
1732 (if (re-search-backward "^[ \t]*[^ \t#\n]" nil t)
1733 (progn (py-goto-initial-line) t)
1734 nil)
Guido van Rossuma7925f11994-01-26 10:20:16 +00001735 t))
1736
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001737;; go to start of first statement (not blank or comment or
1738;; continuation line) following the statement containing point returns
1739;; t if there is one, else nil
Guido van Rossuma7925f11994-01-26 10:20:16 +00001740(defun py-goto-statement-below ()
1741 (beginning-of-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001742 (let ((start (point)))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001743 (py-goto-beyond-final-line)
1744 (while (and
1745 (looking-at py-blank-or-comment-re)
1746 (not (eobp)))
1747 (forward-line 1))
1748 (if (eobp)
1749 (progn (goto-char start) nil)
1750 t)))
1751
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001752;; go to start of statement, at or preceding point, starting with
1753;; keyword KEY. Skips blank lines and non-indenting comments upward
1754;; first. If that statement starts with KEY, done, else go back to
1755;; first enclosing block starting with KEY. If successful, leaves
1756;; point at the start of the KEY line & returns t. Else leaves point
1757;; at an undefined place & returns nil.
Guido van Rossuma7925f11994-01-26 10:20:16 +00001758(defun py-go-up-tree-to-keyword (key)
1759 ;; skip blanks and non-indenting #
1760 (py-goto-initial-line)
1761 (while (and
1762 (looking-at "[ \t]*\\($\\|#[^ \t\n]\\)")
1763 (zerop (forward-line -1))) ; go back
1764 nil)
1765 (py-goto-initial-line)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001766 (let* ((re (concat "[ \t]*" key "\\b"))
1767 (case-fold-search nil) ; let* so looking-at sees this
1768 (found (looking-at re))
1769 (dead nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001770 (while (not (or found dead))
1771 (condition-case nil ; in case no enclosing block
1772 (py-goto-block-up 'no-mark)
1773 (error (setq dead t)))
1774 (or dead (setq found (looking-at re))))
1775 (beginning-of-line)
1776 found))
1777
1778;; return string in buffer from start of indentation to end of line;
1779;; prefix "..." if leading whitespace was skipped
1780(defun py-suck-up-leading-text ()
1781 (save-excursion
1782 (back-to-indentation)
1783 (concat
1784 (if (bolp) "" "...")
1785 (buffer-substring (point) (progn (end-of-line) (point))))))
1786
1787;; assuming point at bolp, return first keyword ([a-z]+) on the line,
1788;; as a Lisp symbol; return nil if none
1789(defun py-suck-up-first-keyword ()
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001790 (let ((case-fold-search nil))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001791 (if (looking-at "[ \t]*\\([a-z]+\\)\\b")
1792 (intern (buffer-substring (match-beginning 1) (match-end 1)))
1793 nil)))
1794
1795(defun py-make-temp-name ()
1796 (make-temp-name
1797 (concat (file-name-as-directory py-temp-directory) "python")))
1798
1799(defun py-delete-file-silently (fname)
1800 (condition-case nil
1801 (delete-file fname)
1802 (error nil)))
1803
1804(defun py-kill-emacs-hook ()
1805 ;; delete our temp files
1806 (while py-file-queue
1807 (py-delete-file-silently (car py-file-queue))
1808 (setq py-file-queue (cdr py-file-queue)))
Guido van Rossum581d1721994-04-28 08:31:52 +00001809 (if (not (or py-this-is-lucid-emacs-p py-this-is-emacs-19-p))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001810 ;; run the hook we inherited, if any
1811 (and py-inherited-kill-emacs-hook
1812 (funcall py-inherited-kill-emacs-hook))))
1813
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001814;; make PROCESS's buffer visible, append STRING to it, and force
1815;; display; also make shell-mode believe the user typed this string,
1816;; so that kill-output-from-shell and show-output-from-shell work
1817;; "right"
Guido van Rossuma7925f11994-01-26 10:20:16 +00001818(defun py-append-to-process-buffer (process string)
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001819 (let ((cbuf (current-buffer))
1820 (pbuf (process-buffer process))
1821 (py-scroll-process-buffer t))
Guido van Rossuma7925f11994-01-26 10:20:16 +00001822 (set-buffer pbuf)
1823 (goto-char (point-max))
1824 (move-marker (process-mark process) (point))
1825 (if (not py-this-is-emacs-19-p)
1826 (move-marker last-input-start (point))) ; muck w/ shell-mode
1827 (funcall (process-filter process) process string)
1828 (if (not py-this-is-emacs-19-p)
1829 (move-marker last-input-end (point))) ; muck w/ shell-mode
1830 (set-buffer cbuf))
1831 (sit-for 0))
1832
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001833(defun py-keep-region-active ()
1834 ;; do whatever is necessary to keep the region active in XEmacs.
1835 ;; Ignore byte-compiler warnings you might see. Also note that
1836 ;; FSF's Emacs 19 does it differently and doesn't its policy doesn't
1837 ;; require us to take explicit action.
1838 (and (boundp 'zmacs-region-stays)
1839 (setq zmacs-region-stays t)))
1840
1841
Guido van Rossum1d5645d1995-03-14 21:31:47 +00001842(defconst py-version "2.11"
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001843 "`python-mode' version number.")
1844(defconst py-help-address "bwarsaw@cnri.reston.va.us"
1845 "Address accepting submission of bug reports.")
1846
1847(defun py-version ()
1848 "Echo the current version of `python-mode' in the minibuffer."
1849 (interactive)
1850 (message "Using `python-mode' version %s" py-version)
1851 (py-keep-region-active))
1852
1853;; only works under Emacs 19
1854;(eval-when-compile
1855; (require 'reporter))
1856
1857(defun py-submit-bug-report (enhancement-p)
1858 "Submit via mail a bug report on `python-mode'.
1859With \\[universal-argument] just submit an enhancement request."
1860 (interactive
1861 (list (not (y-or-n-p
1862 "Is this a bug report? (hit `n' to send other comments) "))))
Guido van Rossum1d5645d1995-03-14 21:31:47 +00001863 (let ((reporter-prompt-for-summary-p (if enhancement-p
1864 "(Very) brief summary: "
1865 t)))
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001866 (require 'reporter)
1867 (reporter-submit-bug-report
1868 py-help-address ;address
Guido van Rossum1d5645d1995-03-14 21:31:47 +00001869 (concat "python-mode " py-version) ;pkgname
Guido van Rossumdeaa1051995-03-10 16:17:03 +00001870 ;; varlist
1871 (if enhancement-p nil
1872 '(py-python-command
1873 py-indent-offset
1874 py-block-comment-prefix
1875 py-scroll-process-buffer
1876 py-temp-directory
1877 py-beep-if-tab-change))
1878 nil ;pre-hooks
1879 nil ;post-hooks
1880 "Dear Barry,") ;salutation
1881 (if enhancement-p nil
1882 (set-mark (point))
1883 (insert
1884"Please replace this text with a sufficiently large code sample\n\
1885and an exact recipe so that I can reproduce your problem. Failure\n\
1886to do so may mean a greater delay in fixing your bug.\n\n")
1887 (exchange-point-and-mark)
1888 (py-keep-region-active))))
1889
1890
1891;; arrange to kill temp files when Emacs exists
1892(if (or py-this-is-emacs-19-p py-this-is-lucid-emacs-p)
1893 (add-hook 'kill-emacs-hook 'py-kill-emacs-hook)
1894 ;; have to trust that other people are as respectful of our hook
1895 ;; fiddling as we are of theirs
1896 (if (boundp 'py-inherited-kill-emacs-hook)
1897 ;; we were loaded before -- trust others not to have screwed us
1898 ;; in the meantime (no choice, really)
1899 nil
1900 ;; else arrange for our hook to run theirs
1901 (setq py-inherited-kill-emacs-hook kill-emacs-hook)
1902 (setq kill-emacs-hook 'py-kill-emacs-hook)))
1903
1904
1905
1906(provide 'python-mode)
1907;;; python-mode.el ends here